summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2020-02-28 16:15:08 (GMT)
committerKitware Robot <kwrobot@kitware.com>2020-02-28 16:15:31 (GMT)
commitd61a99c3ca9439a903a7eac66596bd117f7ece17 (patch)
tree3b7a39839fcaa1c63eb36b36ae6c3bf9fbac6473 /Source
parent402516c72432a9dfdbbc1e0e6a855010521a34c7 (diff)
parent185d1aefaa209115d21c6c821ff85f64411e95de (diff)
downloadCMake-d61a99c3ca9439a903a7eac66596bd117f7ece17.zip
CMake-d61a99c3ca9439a903a7eac66596bd117f7ece17.tar.gz
CMake-d61a99c3ca9439a903a7eac66596bd117f7ece17.tar.bz2
Merge topic 'foreach-range-issues'
185d1aefaa foreach: Set fatal error on invalid range a33b3949e5 foreach: Fix crash when parsing invalid integer Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !4407
Diffstat (limited to 'Source')
-rw-r--r--Source/cmForEachCommand.cxx42
1 files changed, 36 insertions, 6 deletions
diff --git a/Source/cmForEachCommand.cxx b/Source/cmForEachCommand.cxx
index ac48287..0546186 100644
--- a/Source/cmForEachCommand.cxx
+++ b/Source/cmForEachCommand.cxx
@@ -12,6 +12,8 @@
#include <cstdlib>
#include <iterator>
#include <map>
+#include <sstream>
+#include <stdexcept>
#include <utility>
#include <cm/memory>
@@ -354,6 +356,21 @@ bool HandleInMode(std::vector<std::string> const& args,
return true;
}
+bool TryParseInteger(cmExecutionStatus& status, const std::string& str, int& i)
+{
+ try {
+ i = std::stoi(str);
+ } catch (std::invalid_argument&) {
+ std::ostringstream e;
+ e << "Invalid integer: '" << str << "'";
+ status.SetError(e.str());
+ cmSystemTools::SetFatalErrorOccured();
+ return false;
+ }
+
+ return true;
+}
+
} // anonymous namespace
bool cmForEachCommand(std::vector<std::string> const& args,
@@ -376,16 +393,28 @@ bool cmForEachCommand(std::vector<std::string> const& args,
int stop = 0;
int step = 0;
if (args.size() == 3) {
- stop = std::stoi(args[2]);
+ if (!TryParseInteger(status, args[2], stop)) {
+ return false;
+ }
}
if (args.size() == 4) {
- start = std::stoi(args[2]);
- stop = std::stoi(args[3]);
+ if (!TryParseInteger(status, args[2], start)) {
+ return false;
+ }
+ if (!TryParseInteger(status, args[3], stop)) {
+ return false;
+ }
}
if (args.size() == 5) {
- start = std::stoi(args[2]);
- stop = std::stoi(args[3]);
- step = std::stoi(args[4]);
+ if (!TryParseInteger(status, args[2], start)) {
+ return false;
+ }
+ if (!TryParseInteger(status, args[3], stop)) {
+ return false;
+ }
+ if (!TryParseInteger(status, args[4], step)) {
+ return false;
+ }
}
if (step == 0) {
if (start > stop) {
@@ -399,6 +428,7 @@ bool cmForEachCommand(std::vector<std::string> const& args,
status.SetError(
cmStrCat("called with incorrect range specification: start ", start,
", stop ", stop, ", step ", step));
+ cmSystemTools::SetFatalErrorOccured();
return false;
}