diff options
author | Brad King <brad.king@kitware.com> | 2019-04-12 11:58:20 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2019-04-12 11:58:52 (GMT) |
commit | f70367e4ed8a3092cd333acb8bb79735e8307980 (patch) | |
tree | ef02134daff6471e56b4764c498f18211e82c11d | |
parent | d798d2f7c5cc6b3fc69974f8d9392265228dbe5b (diff) | |
parent | f0948499f6b47a7a856aef3334a8d8a38c1265d5 (diff) | |
download | CMake-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.cxx | 6 | ||||
-rw-r--r-- | Tests/CMakeLib/testSystemTools.cxx | 17 |
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; } |