Commit 4e2fb1a5 by Niels Lohmann Committed by GitHub

Merge pull request #390 from qwename/integer-overflow

🔀 fix issue #380: Signed integer overflow check
parents dfafd2c2 703d4baf
...@@ -10619,19 +10619,19 @@ basic_json_parser_66: ...@@ -10619,19 +10619,19 @@ basic_json_parser_66:
// skip if definitely not an integer // skip if definitely not an integer
if (type != value_t::number_float) if (type != value_t::number_float)
{ {
// multiply last value by ten and add the new digit auto digit = static_cast<number_unsigned_t>(*curptr - '0');
auto temp = value * 10 + *curptr - '0';
// test for overflow // overflow if value * 10 + digit > max, move terms around
if (temp < value || temp > max) // to avoid overflow in intermediate values
if (value > (max - digit) / 10)
{ {
// overflow // overflow
type = value_t::number_float; type = value_t::number_float;
} }
else else
{ {
// no overflow - save it // no overflow
value = temp; value = value * 10 + digit;
} }
} }
} }
......
...@@ -9769,19 +9769,19 @@ class basic_json ...@@ -9769,19 +9769,19 @@ class basic_json
// skip if definitely not an integer // skip if definitely not an integer
if (type != value_t::number_float) if (type != value_t::number_float)
{ {
// multiply last value by ten and add the new digit auto digit = static_cast<number_unsigned_t>(*curptr - '0');
auto temp = value * 10 + *curptr - '0';
// test for overflow // overflow if value * 10 + digit > max, move terms around
if (temp < value || temp > max) // to avoid overflow in intermediate values
if (value > (max - digit) / 10)
{ {
// overflow // overflow
type = value_t::number_float; type = value_t::number_float;
} }
else else
{ {
// no overflow - save it // no overflow
value = temp; value = value * 10 + digit;
} }
} }
} }
......
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