Skip to content

Commit

Permalink
Merge pull request #390 from qwename/integer-overflow
Browse files Browse the repository at this point in the history
🔀 fix issue #380: Signed integer overflow check
  • Loading branch information
nlohmann authored Dec 13, 2016
2 parents dfafd2c + 703d4ba commit 4e2fb1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10619,19 +10619,19 @@ class basic_json
// skip if definitely not an integer
if (type != value_t::number_float)
{
// multiply last value by ten and add the new digit
auto temp = value * 10 + *curptr - '0';
auto digit = static_cast<number_unsigned_t>(*curptr - '0');

// test for overflow
if (temp < value || temp > max)
// overflow if value * 10 + digit > max, move terms around
// to avoid overflow in intermediate values
if (value > (max - digit) / 10)
{
// overflow
type = value_t::number_float;
}
else
{
// no overflow - save it
value = temp;
// no overflow
value = value * 10 + digit;
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -9769,19 +9769,19 @@ class basic_json
// skip if definitely not an integer
if (type != value_t::number_float)
{
// multiply last value by ten and add the new digit
auto temp = value * 10 + *curptr - '0';
auto digit = static_cast<number_unsigned_t>(*curptr - '0');

// test for overflow
if (temp < value || temp > max)
// overflow if value * 10 + digit > max, move terms around
// to avoid overflow in intermediate values
if (value > (max - digit) / 10)
{
// overflow
type = value_t::number_float;
}
else
{
// no overflow - save it
value = temp;
// no overflow
value = value * 10 + digit;
}
}
}
Expand Down

0 comments on commit 4e2fb1a

Please sign in to comment.