Skip to content

Commit

Permalink
Fix overflow check
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 12, 2017
1 parent 1d751bc commit 493586c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3758,17 +3758,19 @@ template <typename Char>
unsigned parse_nonnegative_int(const Char *&s) {
assert('0' <= *s && *s <= '9');
unsigned value = 0;
// Convert to unsigned to prevent a warning.
unsigned max_int = (std::numeric_limits<int>::max)();
unsigned big = max_int / 10;
do {
unsigned new_value = value * 10 + (*s++ - '0');
// Check if value wrapped around.
if (new_value < value) {
value = (std::numeric_limits<unsigned>::max)();
// Check for overflow.
if (value > big) {
value = max_int + 1;
break;
}
value = new_value;
value = value * 10 + (*s - '0');
++s;
} while ('0' <= *s && *s <= '9');
// Convert to unsigned to prevent a warning.
unsigned max_int = (std::numeric_limits<int>::max)();
if (value > max_int)
FMT_THROW(FormatError("number is too big"));
return value;
Expand Down

0 comments on commit 493586c

Please sign in to comment.