diff options
author | Brad King <brad.king@kitware.com> | 2017-09-11 15:05:20 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2017-09-13 14:47:04 (GMT) |
commit | 31f73eb12dd0888efc61c0333539d1354c7268ce (patch) | |
tree | 11eddb116df6844f7e534a03f213b7ce07a56fba /Source/cmSystemTools.cxx | |
parent | 3f8c6cab4bb4a9f68708c11a38e4487dad363e38 (diff) | |
download | CMake-31f73eb12dd0888efc61c0333539d1354c7268ce.zip CMake-31f73eb12dd0888efc61c0333539d1354c7268ce.tar.gz CMake-31f73eb12dd0888efc61c0333539d1354c7268ce.tar.bz2 |
get_filename_component: Revise PROGRAM/PROGRAM_ARGS split semantics
The KWSys `SystemTools::SplitProgramFromArgs` implementation goes into
an infinite loop when the value is just " " (a space). Since the
"program path with unquoted spaces plus command-line arguments"
operation it is trying to provide is poorly defined (string parsing
should not depend on filesystem content), just stop using it.
Instead consider the main two use cases the old approach tried to handle:
* The value is the name or absolute path of a program with no quoting
or escaping, but also no command-line arguments. In this case we
can use the value as given with no parsing, and assume no arguments.
* The value is a command-line string containing the program name/path
plus arguments. In this case we now assume that the command line
is properly quoted or escaped.
Fixes: #17262
Diffstat (limited to 'Source/cmSystemTools.cxx')
-rw-r--r-- | Source/cmSystemTools.cxx | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 5c63d98..2635d6d 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -603,6 +603,56 @@ std::vector<std::string> cmSystemTools::ParseArguments(const char* command) return args; } +bool cmSystemTools::SplitProgramFromArgs(std::string const& command, + std::string& program, + std::string& args) +{ + const char* c = command.c_str(); + + // Skip leading whitespace. + while (isspace(static_cast<unsigned char>(*c))) { + ++c; + } + + // Parse one command-line element up to an unquoted space. + bool in_escape = false; + bool in_double = false; + bool in_single = false; + for (; *c; ++c) { + if (in_single) { + if (*c == '\'') { + in_single = false; + } else { + program += *c; + } + } else if (in_escape) { + in_escape = false; + program += *c; + } else if (*c == '\\') { + in_escape = true; + } else if (in_double) { + if (*c == '"') { + in_double = false; + } else { + program += *c; + } + } else if (*c == '"') { + in_double = true; + } else if (*c == '\'') { + in_single = true; + } else if (isspace(static_cast<unsigned char>(*c))) { + break; + } else { + program += *c; + } + } + + // The remainder of the command line holds unparsed arguments. + args = c; + + return !in_single && !in_escape && !in_double; +} + size_t cmSystemTools::CalculateCommandLineLengthLimit() { size_t sz = |