Browse Source

cmListCommand: Handle invalid FOR selector ranges

Fixes crashes involving invalid ranges specified in list(TRANSFORM ...
FOR ...) calls.
* Report error when step is not positive
* Report error when start is after stop

Fixes: #22985
Nick Muggio 3 years ago
parent
commit
7dd3e99270

+ 11 - 2
Source/cmListCommand.cxx

@@ -678,6 +678,14 @@ public:
     this->Start = this->NormalizeIndex(this->Start, count);
     this->Stop = this->NormalizeIndex(this->Stop, count);
 
+    // Does stepping move us further from the end?
+    if (this->Start > this->Stop) {
+      throw transform_error(
+        cmStrCat("sub-command TRANSFORM, selector FOR "
+                 "expects <start> to be no greater than <stop> (",
+                 this->Start, " > ", this->Stop, ")"));
+    }
+
     // compute indexes
     auto size = (this->Stop - this->Start + 1) / this->Step;
     if ((this->Stop - this->Start + 1) % this->Step != 0) {
@@ -1026,9 +1034,10 @@ bool HandleTransformCommand(std::vector<std::string> const& args,
         }
       }
 
-      if (step < 0) {
+      if (step <= 0) {
         status.SetError("sub-command TRANSFORM, selector FOR expects "
-                        "non negative numeric value for <step>.");
+                        "positive numeric value for <step>.");
+        return false;
       }
 
       command.Selector =

+ 3 - 0
Tests/RunCMake/list/RunCMakeTest.cmake

@@ -77,6 +77,9 @@ run_cmake(TRANSFORM-Selector-FOR-NoEnoughArguments)
 run_cmake(TRANSFORM-Selector-FOR-TooManyArguments)
 run_cmake(TRANSFORM-Selector-FOR-BadArgument)
 run_cmake(TRANSFORM-Selector-FOR-InvalidIndex)
+run_cmake(TRANSFORM-Selector-FOR-ZeroStepArgument)
+run_cmake(TRANSFORM-Selector-FOR-NegativeStepArgument)
+run_cmake(TRANSFORM-Selector-FOR-BackwardsRange)
 # 'output' oriented tests
 run_cmake(TRANSFORM-Output-OUTPUT_VARIABLE-NoArguments)
 run_cmake(TRANSFORM-Output-OUTPUT_VARIABLE-TooManyArguments)

+ 1 - 0
Tests/RunCMake/list/TRANSFORM-Selector-FOR-BackwardsRange-result.txt

@@ -0,0 +1 @@
+1

+ 5 - 0
Tests/RunCMake/list/TRANSFORM-Selector-FOR-BackwardsRange-stderr.txt

@@ -0,0 +1,5 @@
+^CMake Error at TRANSFORM-Selector-FOR-BackwardsRange.cmake:2 \(list\):
+  list sub-command TRANSFORM, selector FOR expects <start> to be no greater
+  than <stop> \(2 > 1\)
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)$

+ 2 - 0
Tests/RunCMake/list/TRANSFORM-Selector-FOR-BackwardsRange.cmake

@@ -0,0 +1,2 @@
+set(mylist alpha bravo charlie)
+list(TRANSFORM mylist TOUPPER FOR 2 1)

+ 1 - 0
Tests/RunCMake/list/TRANSFORM-Selector-FOR-NegativeStepArgument-result.txt

@@ -0,0 +1 @@
+1

+ 5 - 0
Tests/RunCMake/list/TRANSFORM-Selector-FOR-NegativeStepArgument-stderr.txt

@@ -0,0 +1,5 @@
+^CMake Error at TRANSFORM-Selector-FOR-NegativeStepArgument.cmake:2 \(list\):
+  list sub-command TRANSFORM, selector FOR expects positive numeric value for
+  <step>.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)$

+ 2 - 0
Tests/RunCMake/list/TRANSFORM-Selector-FOR-NegativeStepArgument.cmake

@@ -0,0 +1,2 @@
+set(mylist alpha bravo charlie)
+list(TRANSFORM mylist TOUPPER FOR 0 2 -1)

+ 1 - 0
Tests/RunCMake/list/TRANSFORM-Selector-FOR-ZeroStepArgument-result.txt

@@ -0,0 +1 @@
+1

+ 5 - 0
Tests/RunCMake/list/TRANSFORM-Selector-FOR-ZeroStepArgument-stderr.txt

@@ -0,0 +1,5 @@
+^CMake Error at TRANSFORM-Selector-FOR-ZeroStepArgument.cmake:2 \(list\):
+  list sub-command TRANSFORM, selector FOR expects positive numeric value for
+  <step>.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)$

+ 2 - 0
Tests/RunCMake/list/TRANSFORM-Selector-FOR-ZeroStepArgument.cmake

@@ -0,0 +1,2 @@
+set(mylist alpha bravo charlie)
+list(TRANSFORM mylist TOUPPER FOR 0 2 0)