summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2019-04-12 11:58:20 (GMT)
committerKitware Robot <kwrobot@kitware.com>2019-04-12 11:58:52 (GMT)
commitf70367e4ed8a3092cd333acb8bb79735e8307980 (patch)
treeef02134daff6471e56b4764c498f18211e82c11d
parentd798d2f7c5cc6b3fc69974f8d9392265228dbe5b (diff)
parentf0948499f6b47a7a856aef3334a8d8a38c1265d5 (diff)
downloadCMake-f70367e4ed8a3092cd333acb8bb79735e8307980.zip
CMake-f70367e4ed8a3092cd333acb8bb79735e8307980.tar.gz
CMake-f70367e4ed8a3092cd333acb8bb79735e8307980.tar.bz2
Merge topic 'cmSystemTools-StringToULong-negatives'
f0948499f6 cmSystemTools: Fix StringToULong to reject negative numbers Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !3216
-rw-r--r--Source/cmSystemTools.cxx6
-rw-r--r--Tests/CMakeLib/testSystemTools.cxx17
2 files changed, 23 insertions, 0 deletions
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx
index d201061..212608d 100644
--- a/Source/cmSystemTools.cxx
+++ b/Source/cmSystemTools.cxx
@@ -3023,6 +3023,12 @@ bool cmSystemTools::StringToULong(const char* str, unsigned long* value)
{
errno = 0;
char* endp;
+ while (isspace(*str)) {
+ ++str;
+ }
+ if (*str == '-') {
+ return false;
+ }
*value = strtoul(str, &endp, 10);
return (*endp == '\0') && (endp != str) && (errno == 0);
}
diff --git a/Tests/CMakeLib/testSystemTools.cxx b/Tests/CMakeLib/testSystemTools.cxx
index 96a4819..121e639 100644
--- a/Tests/CMakeLib/testSystemTools.cxx
+++ b/Tests/CMakeLib/testSystemTools.cxx
@@ -93,5 +93,22 @@ int testSystemTools(int /*unused*/, char* /*unused*/ [])
if (!failed) {
cmPassed("cmSystemTools::strverscmp working");
}
+
+ // ----------------------------------------------------------------------
+ // Test cmSystemTools::StringToULong
+ {
+ unsigned long value;
+ cmAssert(cmSystemTools::StringToULong("1", &value) && value == 1,
+ "StringToULong parses a decimal integer.");
+ cmAssert(cmSystemTools::StringToULong(" 1", &value) && value == 1,
+ "StringToULong parses a decimal integer after whitespace.");
+ cmAssert(!cmSystemTools::StringToULong("-1", &value),
+ "StringToULong rejects a negative number.");
+ cmAssert(!cmSystemTools::StringToULong(" -1", &value),
+ "StringToULong rejects a negative number after whitespace.");
+ cmAssert(!cmSystemTools::StringToULong("1x", &value),
+ "StringToULong rejects trailing content.");
+ }
+
return failed;
}