From 6e594916592e5f8f17b387546d79e484ac092b9e Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 14 May 2018 11:49:02 -0400 Subject: add_custom_{command,target}: Fix crash on empty expanded command Our custom command generation logic assumes that all command lines have at least `argv0`. In `add_custom_{command,target}` we already check that at least a `COMMAND` was given, but using `COMMAND_EXPAND_LISTS` in combination with a generator expression that expands to an empty string may produce an empty command line. In this case simply add an empty string as a command to maintain our internal invariant. Fixes: #17993 --- Source/cmCustomCommandGenerator.cxx | 8 ++++++++ Tests/RunCMake/add_custom_target/CommandExpandsEmpty.cmake | 1 + Tests/RunCMake/add_custom_target/RunCMakeTest.cmake | 1 + 3 files changed, 10 insertions(+) create mode 100644 Tests/RunCMake/add_custom_target/CommandExpandsEmpty.cmake diff --git a/Source/cmCustomCommandGenerator.cxx b/Source/cmCustomCommandGenerator.cxx index 136cf39..6c9f9d6 100644 --- a/Source/cmCustomCommandGenerator.cxx +++ b/Source/cmCustomCommandGenerator.cxx @@ -40,6 +40,14 @@ cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc, argv.push_back(std::move(parsed_arg)); } } + + // Later code assumes at least one entry exists, but expanding + // lists on an empty command may have left this empty. + // FIXME: Should we define behavior for removing empty commands? + if (argv.empty()) { + argv.push_back(std::string()); + } + this->CommandLines.push_back(std::move(argv)); } diff --git a/Tests/RunCMake/add_custom_target/CommandExpandsEmpty.cmake b/Tests/RunCMake/add_custom_target/CommandExpandsEmpty.cmake new file mode 100644 index 0000000..bc899a4 --- /dev/null +++ b/Tests/RunCMake/add_custom_target/CommandExpandsEmpty.cmake @@ -0,0 +1 @@ +add_custom_target(EmptyCustom COMMAND "" COMMAND_EXPAND_LISTS) diff --git a/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake b/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake index 6c4e91b..2caed03 100644 --- a/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake +++ b/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake @@ -1,5 +1,6 @@ include(RunCMake) +run_cmake(CommandExpandsEmpty) run_cmake(NoArguments) run_cmake(BadTargetName) run_cmake(ByproductsNoCommand) -- cgit v0.12