mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-26 00:21:13 +00:00
Fix overflow check
This commit is contained in:
parent
1d751bc617
commit
493586cbca
20
fmt/format.h
20
fmt/format.h
@ -3758,17 +3758,19 @@ template <typename Char>
|
||||
unsigned parse_nonnegative_int(const Char *&s) {
|
||||
assert('0' <= *s && *s <= '9');
|
||||
unsigned value = 0;
|
||||
do {
|
||||
unsigned new_value = value * 10 + (*s++ - '0');
|
||||
// Check if value wrapped around.
|
||||
if (new_value < value) {
|
||||
value = (std::numeric_limits<unsigned>::max)();
|
||||
break;
|
||||
}
|
||||
value = new_value;
|
||||
} while ('0' <= *s && *s <= '9');
|
||||
// Convert to unsigned to prevent a warning.
|
||||
unsigned max_int = (std::numeric_limits<int>::max)();
|
||||
unsigned big = max_int / 10;
|
||||
do {
|
||||
// Check for overflow.
|
||||
if (value > big) {
|
||||
value = max_int + 1;
|
||||
break;
|
||||
}
|
||||
value = value * 10 + (*s - '0');
|
||||
++s;
|
||||
} while ('0' <= *s && *s <= '9');
|
||||
// Convert to unsigned to prevent a warning.
|
||||
if (value > max_int)
|
||||
FMT_THROW(FormatError("number is too big"));
|
||||
return value;
|
||||
|
Loading…
Reference in New Issue
Block a user