Fixed range check on TryParse() for u32, again.

The code from 748be364e5 incorrectly accepted -0x100000000 on x86_64.

Also if ERANGE is returned by strtoul(), reject the parsed value regardless
of what that value is. This fixes invalid values being returned when compiling
with Visual C++. Thanks to "cotton" for testing this.
This commit is contained in:
Maarten ter Huurne 2011-12-13 02:02:31 +01:00
parent f3325036be
commit 27bda2c054
1 changed files with 4 additions and 3 deletions

View File

@ -130,12 +130,13 @@ bool TryParse(const std::string &str, u32 *const output)
if (!endptr || *endptr)
return false;
if (value == ULONG_MAX && errno == ERANGE)
if (errno == ERANGE)
return false;
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)
// Note: The typecasts avoid GCC warnings when long is 32 bits wide.
if (value >= static_cast<unsigned long>(0x100000000ull)
&& value <= static_cast<unsigned long>(0xFFFFFFFF00000000ull))
return false;
}