1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-16 05:26:58 +08:00

file(GENERATE): Restore INPUT|CONTENT parse checking

Refactoring in commit bff468c988 (cmFileCommand: Use cm::optional for
keyword argument presence, 2022-06-30, v3.25.0-rc1~512^2) accidentally
broke the check that the input argument is either `INPUT` or `CONTENT`.

The check is supposed to fail when arguments are passed in the wrong
order.  For example:

    file(GENERATE OUTPUT ...
         TARGET <target>
         CONTENT <content>)

Prior to this fix, the input method would be CONTENT, but because the
first parsed keyword is not `CONTENT`, `inputIsContent` would be false.
The first parsed keyword isn't INPUT either, so we would not continue
into the error condition. CMake would then try to handle this as an
input file, when there isn't one, resulting in uninitialized memory
usage and segfaults or corruption later on.

Fixes: #25169
This commit is contained in:
Evan Wilde
2023-08-07 11:14:14 -07:00
committed by Brad King
parent cbcd297826
commit 88f90a72f1
5 changed files with 12 additions and 1 deletions

View File

@@ -2555,8 +2555,9 @@ bool HandleGenerateCommand(std::vector<std::string> const& args,
return false;
}
const bool inputIsContent = arguments.ParsedKeywords[1] == "CONTENT"_s;
if (!inputIsContent && arguments.ParsedKeywords[1] == "INPUT") {
if (!inputIsContent && arguments.ParsedKeywords[1] != "INPUT") {
status.SetError("Unknown argument to GENERATE subcommand.");
return false;
}
std::string const& input =
inputIsContent ? *arguments.Content : *arguments.Input;

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,4 @@
^CMake Error at OutOfOrderArgs\.cmake:[0-9]+ \(file\):
file Unknown argument to GENERATE subcommand\.
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$

View File

@@ -0,0 +1,4 @@
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/output.txt"
CONDITION 1
CONTENT "CONTENT argument"
)

View File

@@ -15,6 +15,7 @@ endif()
run_cmake(EmptyCondition1)
run_cmake(EmptyCondition2)
run_cmake(BadCondition)
run_cmake(OutOfOrderArgs)
run_cmake(DebugEvaluate)
run_cmake(GenerateSource)
run_cmake(InputAndContent)