cmSystemTools: Fix StringToULong to reject negative numbers

Fixes: #19161
This commit is contained in:
Brad King
2019-04-11 12:56:32 -04:00
parent a550e2d6e4
commit f0948499f6
2 changed files with 23 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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;
}