From 27bda2c054f38cdd8b6a04fe939ea006535ae532 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 13 Dec 2011 02:02:31 +0100 Subject: [PATCH] Fixed range check on TryParse() for u32, again. The code from 748be364e5ee 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. --- Source/Core/Common/Src/StringUtil.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Core/Common/Src/StringUtil.cpp b/Source/Core/Common/Src/StringUtil.cpp index 94f01e66f8..cdd1016210 100644 --- a/Source/Core/Common/Src/StringUtil.cpp +++ b/Source/Core/Common/Src/StringUtil.cpp @@ -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(0x100000000ull) + && value <= static_cast(0xFFFFFFFF00000000ull)) return false; }