Skip to content

Commit

Permalink
JSON: add logic to detect overflow using gcc __builtin_ overflow dete…
Browse files Browse the repository at this point in the history
…ction method
  • Loading branch information
HaseenaSainul committed Aug 19, 2024
1 parent 41fb4e4 commit 48b37c2
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions Source/core/JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,17 +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');
loaded++;
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 @@ -769,8 +781,12 @@ namespace Core {
completed = true;
}

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

0 comments on commit 48b37c2

Please sign in to comment.