diff options
author | Ben Boeckel <ben.boeckel@kitware.com> | 2021-02-16 15:11:02 (GMT) |
---|---|---|
committer | Ben Boeckel <ben.boeckel@kitware.com> | 2021-02-16 15:43:43 (GMT) |
commit | 9934a97642f82f35380fca7b5b1b1867abbd9688 (patch) | |
tree | 55449af3ea5b21fd0a53f56533279641290d8c6b | |
parent | 6e2e906365b695d0c9908ef9535527c74107adcf (diff) | |
download | CMake-9934a97642f82f35380fca7b5b1b1867abbd9688.zip CMake-9934a97642f82f35380fca7b5b1b1867abbd9688.tar.gz CMake-9934a97642f82f35380fca7b5b1b1867abbd9688.tar.bz2 |
cmListCommand: refactor out index argument parsing
This is in preparation for detecting errors in index argument syntax.
-rw-r--r-- | Source/cmListCommand.cxx | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/Source/cmListCommand.cxx b/Source/cmListCommand.cxx index fdddb45..edfaeec 100644 --- a/Source/cmListCommand.cxx +++ b/Source/cmListCommand.cxx @@ -36,6 +36,13 @@ namespace { +bool GetIndexArg(char const* arg, int* idx) +{ + *idx = atoi(arg); + // Ignore errors. + return true; +} + bool FilterRegex(std::vector<std::string> const& args, bool includeMatches, std::string const& listName, std::vector<std::string>& varArgsExpanded, @@ -154,7 +161,11 @@ bool HandleGetCommand(std::vector<std::string> const& args, const char* sep = ""; size_t nitem = varArgsExpanded.size(); for (cc = 2; cc < args.size() - 1; cc++) { - int item = atoi(args[cc].c_str()); + int item; + if (!GetIndexArg(args[cc].c_str(), &item)) { + status.SetError(cmStrCat("index: ", args[cc], " is not a valid index")); + return false; + } value += sep; sep = ";"; if (item < 0) { @@ -362,7 +373,11 @@ bool HandleInsertCommand(std::vector<std::string> const& args, const std::string& listName = args[1]; // expand the variable - int item = atoi(args[2].c_str()); + int item; + if (!GetIndexArg(args[2].c_str(), &item)) { + status.SetError(cmStrCat("index: ", args[2], " is not a valid index")); + return false; + } std::vector<std::string> varArgsExpanded; if ((!GetList(varArgsExpanded, listName, status.GetMakefile()) || varArgsExpanded.empty()) && @@ -1282,8 +1297,16 @@ bool HandleSublistCommand(std::vector<std::string> const& args, return true; } - const int start = atoi(args[2].c_str()); - const int length = atoi(args[3].c_str()); + int start; + int length; + if (!GetIndexArg(args[2].c_str(), &start)) { + status.SetError(cmStrCat("index: ", args[2], " is not a valid index")); + return false; + } + if (!GetIndexArg(args[3].c_str(), &length)) { + status.SetError(cmStrCat("index: ", args[3], " is not a valid index")); + return false; + } using size_type = decltype(varArgsExpanded)::size_type; @@ -1338,7 +1361,11 @@ bool HandleRemoveAtCommand(std::vector<std::string> const& args, std::vector<size_t> removed; size_t nitem = varArgsExpanded.size(); for (cc = 2; cc < args.size(); ++cc) { - int item = atoi(args[cc].c_str()); + int item; + if (!GetIndexArg(args[cc].c_str(), &item)) { + status.SetError(cmStrCat("index: ", args[cc], " is not a valid index")); + return false; + } if (item < 0) { item = static_cast<int>(nitem) + item; } |