Skip to content

Commit

Permalink
JSON: add error logs for integer overflow case (#1713)
Browse files Browse the repository at this point in the history
* JSON: add error logs for integer overflow case

* JSON: add logic to detect overflow using gcc __builtin_ overflow detection method

---------

Co-authored-by: Pierre Wielders <[email protected]>
Co-authored-by: Mateusz Daniluk <[email protected]>
  • Loading branch information
3 people authored Aug 20, 2024
1 parent 90d7fa0 commit c0a2d2c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Source/core/JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,13 +744,29 @@ namespace Core {
bool completed = ((_set & (ERROR|UNDEFINED)) != 0);

while ((loaded < maxLength) && (completed == false)) {
#ifdef __WINDOWS__
TYPE previous = _value;
TYPE current = _value;
#else
bool overflow = false;
#endif
if (isdigit(stream[loaded])) {
#ifdef __WINDOWS__
_value *= (_set & 0x1F);
_value += (stream[loaded] - '0');
current = _value / (_set & 0x1F);
#else
overflow = __builtin_mul_overflow(_value, (_set & 0x1F), &_value) || __builtin_add_overflow(stream[loaded] - '0', _value, &_value);
#endif
loaded++;
} else if (isxdigit(stream[loaded])) {
#ifdef __WINDOWS__
_value *= 16;
_value += (::toupper(stream[loaded]) - 'A') + 10;
current = _value / 16;
#else
overflow = __builtin_mul_overflow(_value, 16, &_value) || __builtin_add_overflow((::toupper(stream[loaded]) - 'A') + 10, _value, &_value);
#endif
loaded++;
} else if (((_set & QUOTED) != 0) && (stream[loaded] == '\"')) {
completed = true;
Expand All @@ -764,6 +780,16 @@ namespace Core {
_set |= ERROR;
completed = true;
}

#ifdef __WINDOWS__
if (previous != current) {
#else
if (overflow == true) {
#endif
error = Error{ "Integer overflow, it should be in the integer type range" };
_set |= ERROR;
completed = true;
}
}

if ((_set & (ERROR | QUOTED)) == (ERROR | QUOTED)) {
Expand Down

0 comments on commit c0a2d2c

Please sign in to comment.