Fixed range check on TryParse() for u32.
On x86-64, "unsigned long" is 64 bits wide, so it is possible for a number to not trigger a range error on strtoul() but still not fit inside an u32. An extra check is added to ensure that 32-bit and 64-bit builds will accept the same numbers.
This commit is contained in:
parent
045eb924d9
commit
748be364e5
|
@ -125,7 +125,7 @@ std::string StripQuotes(const std::string& s)
|
||||||
bool TryParse(const std::string &str, u32 *const output)
|
bool TryParse(const std::string &str, u32 *const output)
|
||||||
{
|
{
|
||||||
char *endptr = NULL;
|
char *endptr = NULL;
|
||||||
u32 value = strtoul(str.c_str(), &endptr, 0);
|
unsigned long value = strtoul(str.c_str(), &endptr, 0);
|
||||||
|
|
||||||
if (!endptr || *endptr)
|
if (!endptr || *endptr)
|
||||||
return false;
|
return false;
|
||||||
|
@ -133,7 +133,13 @@ bool TryParse(const std::string &str, u32 *const output)
|
||||||
if (value == ULONG_MAX && errno == ERANGE)
|
if (value == ULONG_MAX && errno == ERANGE)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
*output = value;
|
if (ULONG_MAX > UINT_MAX) {
|
||||||
|
// Leading bits must be either all 0 or all 1.
|
||||||
|
if ((~value | UINT_MAX) != ULONG_MAX && (value | UINT_MAX) != ULONG_MAX)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
*output = static_cast<u32>(value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue