From 79d7bcaf4029ce7b78abfcbcaf8cd1a09a172437 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Wed, 24 Mar 2021 14:03:23 -0400 Subject: cmCommandLineArgument: correctly compute next parse index Fixes #21966 Correctly compute the next parse index for `ZeroOrOne` when it is zero, and for OneOrMore. --- Source/cmCommandLineArgument.h | 14 +++++++++----- ...uild-jobs-no-number-trailing--invalid-target-result.txt | 1 + ...uild-jobs-no-number-trailing--invalid-target-stderr.txt | 1 + Tests/RunCMake/CommandLine/RunCMakeTest.cmake | 4 ++++ 4 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build-jobs-no-number-trailing--invalid-target-result.txt create mode 100644 Tests/RunCMake/CommandLine/BuildDir--build-jobs-no-number-trailing--invalid-target-stderr.txt diff --git a/Source/cmCommandLineArgument.h b/Source/cmCommandLineArgument.h index cbedf0a..495dc69 100644 --- a/Source/cmCommandLineArgument.h +++ b/Source/cmCommandLineArgument.h @@ -76,8 +76,9 @@ struct cmCommandLineArgument } else if (this->Type == Values::One || this->Type == Values::ZeroOrOne) { if (input.size() == this->Name.size()) { - ++index; - if (index >= allArgs.size() || allArgs[index][0] == '-') { + auto nextValueIndex = index + 1; + if (nextValueIndex >= allArgs.size() || + allArgs[nextValueIndex][0] == '-') { if (this->Type == Values::ZeroOrOne) { parseState = this->StoreCall(std::string{}, std::forward(state)...) @@ -87,10 +88,11 @@ struct cmCommandLineArgument parseState = ParseMode::ValueError; } } else { - parseState = - this->StoreCall(allArgs[index], std::forward(state)...) + parseState = this->StoreCall(allArgs[nextValueIndex], + std::forward(state)...) ? ParseMode::Valid : ParseMode::Invalid; + index = nextValueIndex; } } else { // parse the string to get the value @@ -133,7 +135,8 @@ struct cmCommandLineArgument } else if (this->Type == Values::OneOrMore) { if (input.size() == this->Name.size()) { auto nextValueIndex = index + 1; - if (nextValueIndex >= allArgs.size() || allArgs[index + 1][0] == '-') { + if (nextValueIndex >= allArgs.size() || + allArgs[nextValueIndex][0] == '-') { parseState = ParseMode::ValueError; } else { std::string buffer = allArgs[nextValueIndex++]; @@ -145,6 +148,7 @@ struct cmCommandLineArgument this->StoreCall(buffer, std::forward(state)...) ? ParseMode::Valid : ParseMode::Invalid; + index = (nextValueIndex - 1); } } } diff --git a/Tests/RunCMake/CommandLine/BuildDir--build-jobs-no-number-trailing--invalid-target-result.txt b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-no-number-trailing--invalid-target-result.txt new file mode 100644 index 0000000..3beecb0 --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-no-number-trailing--invalid-target-result.txt @@ -0,0 +1 @@ +(1|2) diff --git a/Tests/RunCMake/CommandLine/BuildDir--build-jobs-no-number-trailing--invalid-target-stderr.txt b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-no-number-trailing--invalid-target-stderr.txt new file mode 100644 index 0000000..ee4bcc0 --- /dev/null +++ b/Tests/RunCMake/CommandLine/BuildDir--build-jobs-no-number-trailing--invalid-target-stderr.txt @@ -0,0 +1 @@ +.*(ninja: error: unknown target 'invalid-target'|No rule to make target [`']invalid-target').* diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index 51754fc..8072a2c 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake @@ -209,6 +209,10 @@ function(run_BuildDir) ${CMAKE_COMMAND} --build BuildDir-build -j) run_cmake_command(BuildDir--build-jobs-no-number-trailing--target ${CMAKE_COMMAND} -E chdir .. ${CMAKE_COMMAND} --build BuildDir-build -j --target CustomTarget) + if(RunCMake_GENERATOR MATCHES "Unix Makefiles" OR RunCMake_GENERATOR MATCHES "Ninja") + run_cmake_command(BuildDir--build-jobs-no-number-trailing--invalid-target ${CMAKE_COMMAND} -E chdir .. + ${CMAKE_COMMAND} --build BuildDir-build -j --target invalid-target) + endif() run_cmake_command(BuildDir--build--parallel-no-number ${CMAKE_COMMAND} -E chdir .. ${CMAKE_COMMAND} --build BuildDir-build --parallel) run_cmake_command(BuildDir--build--parallel-no-number-trailing--target ${CMAKE_COMMAND} -E chdir .. -- cgit v0.12