summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Src
diff options
context:
space:
mode:
authorMaarten ter Huurne <maarten@treewalker.org>2011-12-02 01:16:56 +0100
committerMaarten ter Huurne <maarten@treewalker.org>2011-12-02 02:24:43 +0100
commit748be364e5ee85cf92dfd0300468afc6bc36cbd2 (patch)
treed66cf6f650af77d4c1538647693527d8745da44e /Source/Core/Common/Src
parent045eb924d93b4532d5a91c8eab55e292e3a997ae (diff)
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.
Diffstat (limited to 'Source/Core/Common/Src')
-rw-r--r--Source/Core/Common/Src/StringUtil.cpp10
1 files changed, 8 insertions, 2 deletions
diff --git a/Source/Core/Common/Src/StringUtil.cpp b/Source/Core/Common/Src/StringUtil.cpp
index 18a3575086..94f01e66f8 100644
--- a/Source/Core/Common/Src/StringUtil.cpp
+++ b/Source/Core/Common/Src/StringUtil.cpp
@@ -125,7 +125,7 @@ std::string StripQuotes(const std::string& s)
bool TryParse(const std::string &str, u32 *const output)
{
char *endptr = NULL;
- u32 value = strtoul(str.c_str(), &endptr, 0);
+ unsigned long value = strtoul(str.c_str(), &endptr, 0);
if (!endptr || *endptr)
return false;
@@ -133,7 +133,13 @@ bool TryParse(const std::string &str, u32 *const output)
if (value == ULONG_MAX && errno == ERANGE)
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;
}