From f0948499f6b47a7a856aef3334a8d8a38c1265d5 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 11 Apr 2019 12:56:32 -0400 Subject: cmSystemTools: Fix StringToULong to reject negative numbers Fixes: #19161 --- Source/cmSystemTools.cxx | 6 ++++++ Tests/CMakeLib/testSystemTools.cxx | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) 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; } -- cgit v0.12