Repository navigation
fix: signed overflow when parsing long numbers - #41
Merged
Merged
Conversation
Integer, fraction and exponent digits were accumulated in lwjson_int_t without a range check. This is undefined behavior and gives wrong values, e.g. 20000000000000000000.5 parsed as 1.55e18, and the valid minimum -9223372036854775808 overflowed before negation. - Integer part is accumulated as a negative value so the minimum fits. If it does not fit lwjson_int_t, the number is returned as real. - Fraction digits that do not fit are ignored, they are below real precision anyway. - Exponent saturates, the result is already 0 or infinity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#33 was closed, but on current
develop(bbcf520)prv_parse_number()still accumulates the integer, fraction and exponent digits inlwjson_int_twithout a range check. Besides the UBSan reports, it gives wrong values in a normal-O2build:Changes, all inside
prv_parse_number():-9223372036854775808fits. If the number doesn't fitlwjson_int_tit is returned asLWJSON_TYPE_NUM_REALinstead of wrapping. The real value is accumulated alongside, so a long integer part before.oreis right too.dec_numwould overflow. Those digits are below double precision anyway (with a 32-bitlwjson_int_tthis keeps 9 fraction digits, before it wrapped).LWJSON_INT_MAXis computed fromsizeof(lwjson_int_t), since the int type is configurable.For numbers that already fit, the output is unchanged: I parsed 60k random numbers (various integer/fraction/exponent lengths, signs,
-0cases) with the old and new code, with bothfloatanddoubleasLWJSON_CFG_REAL_TYPE, and got identical results. I also ran a differential test against Python'sjsonwith random documents under ASan/UBSan; the number related reports are gone. WithLWJSON_CFG_INT_TYPE intthe limits behave the same way.I added a few number cases to
test_json_parse_basic(they fail before this change). All tests intests/pass with ASan/UBSan, I built them with gcc directly rather than throughtest.py.