From c0a2d2c6b14396a305eb1580c6e063f593df9ee8 Mon Sep 17 00:00:00 2001 From: HaseenaSainul <41037131+HaseenaSainul@users.noreply.github.com> Date: Tue, 20 Aug 2024 18:41:16 +0530 Subject: [PATCH] JSON: add error logs for integer overflow case (#1713) * 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 Co-authored-by: Mateusz Daniluk <121170681+VeithMetro@users.noreply.github.com> --- Source/core/JSON.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Source/core/JSON.h b/Source/core/JSON.h index 10d7c5b15..c4ba49f78 100644 --- a/Source/core/JSON.h +++ b/Source/core/JSON.h @@ -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; @@ -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)) {