|
| 1 | +From 0b2b454deee65e0c6627f68de5662d3985903a90 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Tyler Yankee < [email protected]> |
| 3 | +Date: Wed, 13 Aug 2025 15:22:28 -0400 |
| 4 | +Subject: [PATCH] foreach: Explicitly skip replay without iterations |
| 5 | + |
| 6 | +As written, foreach loops with a trailing `IN` (i.e., no loop |
| 7 | +variable(s) given) lead to an assertion error. Handle this case by |
| 8 | +exiting early when we know the loop won't execute anything. |
| 9 | + |
| 10 | +Fixes: #27135 |
| 11 | +Signed-off-by: Azure Linux Security Servicing Account < [email protected]> |
| 12 | +Upstream-reference: https://gitlab.kitware.com/cmake/cmake/-/commit/37e27f71bc356d880c908040cd0cb68fa2c371b8.patch |
| 13 | +--- |
| 14 | + Source/cmForEachCommand.cxx | 3 +++ |
| 15 | + Tests/RunCMake/foreach/RunCMakeTest.cmake | 1 + |
| 16 | + Tests/RunCMake/foreach/TrailingIn-result.txt | 1 + |
| 17 | + Tests/RunCMake/foreach/TrailingIn.cmake | 5 +++++ |
| 18 | + 4 files changed, 10 insertions(+) |
| 19 | + create mode 100644 Tests/RunCMake/foreach/TrailingIn-result.txt |
| 20 | + create mode 100644 Tests/RunCMake/foreach/TrailingIn.cmake |
| 21 | + |
| 22 | +diff --git a/Source/cmForEachCommand.cxx b/Source/cmForEachCommand.cxx |
| 23 | +index 33dae79f..9b9a60b3 100644 |
| 24 | +--- a/Source/cmForEachCommand.cxx |
| 25 | ++++ b/Source/cmForEachCommand.cxx |
| 26 | +@@ -101,6 +101,9 @@ bool cmForEachFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff, |
| 27 | + bool cmForEachFunctionBlocker::Replay( |
| 28 | + std::vector<cmListFileFunction> functions, cmExecutionStatus& inStatus) |
| 29 | + { |
| 30 | ++ if (this->Args.size() == this->IterationVarsCount) { |
| 31 | ++ return true; |
| 32 | ++ } |
| 33 | + return this->ZipLists ? this->ReplayZipLists(functions, inStatus) |
| 34 | + : this->ReplayItems(functions, inStatus); |
| 35 | + } |
| 36 | +diff --git a/Tests/RunCMake/foreach/RunCMakeTest.cmake b/Tests/RunCMake/foreach/RunCMakeTest.cmake |
| 37 | +index 15ca4770..acfc742e 100644 |
| 38 | +--- a/Tests/RunCMake/foreach/RunCMakeTest.cmake |
| 39 | ++++ b/Tests/RunCMake/foreach/RunCMakeTest.cmake |
| 40 | +@@ -22,3 +22,4 @@ run_cmake(foreach-RANGE-invalid-test) |
| 41 | + run_cmake(foreach-RANGE-out-of-range-test) |
| 42 | + run_cmake(foreach-var-scope-CMP0124-OLD) |
| 43 | + run_cmake(foreach-var-scope-CMP0124-NEW) |
| 44 | ++run_cmake(TrailingIn) |
| 45 | +diff --git a/Tests/RunCMake/foreach/TrailingIn-result.txt b/Tests/RunCMake/foreach/TrailingIn-result.txt |
| 46 | +new file mode 100644 |
| 47 | +index 00000000..573541ac |
| 48 | +--- /dev/null |
| 49 | ++++ b/Tests/RunCMake/foreach/TrailingIn-result.txt |
| 50 | +@@ -0,0 +1 @@ |
| 51 | ++0 |
| 52 | +diff --git a/Tests/RunCMake/foreach/TrailingIn.cmake b/Tests/RunCMake/foreach/TrailingIn.cmake |
| 53 | +new file mode 100644 |
| 54 | +index 00000000..e2b5b2f2 |
| 55 | +--- /dev/null |
| 56 | ++++ b/Tests/RunCMake/foreach/TrailingIn.cmake |
| 57 | +@@ -0,0 +1,5 @@ |
| 58 | ++foreach(v IN) |
| 59 | ++endforeach() |
| 60 | ++ |
| 61 | ++foreach(v1 v2 IN) |
| 62 | ++endforeach() |
| 63 | +-- |
| 64 | +2.45.4 |
| 65 | + |
0 commit comments