Commit 493586cb authored by Victor Zverovich's avatar Victor Zverovich

Fix overflow check

parent 1d751bc6
...@@ -3758,17 +3758,19 @@ template <typename Char> ...@@ -3758,17 +3758,19 @@ template <typename Char>
unsigned parse_nonnegative_int(const Char *&s) { unsigned parse_nonnegative_int(const Char *&s) {
assert('0' <= *s && *s <= '9'); assert('0' <= *s && *s <= '9');
unsigned value = 0; unsigned value = 0;
// Convert to unsigned to prevent a warning.
unsigned max_int = (std::numeric_limits<int>::max)();
unsigned big = max_int / 10;
do { do {
unsigned new_value = value * 10 + (*s++ - '0'); // Check for overflow.
// Check if value wrapped around. if (value > big) {
if (new_value < value) { value = max_int + 1;
value = (std::numeric_limits<unsigned>::max)();
break; break;
} }
value = new_value; value = value * 10 + (*s - '0');
++s;
} while ('0' <= *s && *s <= '9'); } while ('0' <= *s && *s <= '9');
// Convert to unsigned to prevent a warning. // Convert to unsigned to prevent a warning.
unsigned max_int = (std::numeric_limits<int>::max)();
if (value > max_int) if (value > max_int)
FMT_THROW(FormatError("number is too big")); FMT_THROW(FormatError("number is too big"));
return value; return value;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment