Repository navigation
Check the whole RFC 8259 number grammar in strict mode - #957
Merged
Merged
Conversation
Strict mode rejected leading zeros but nothing else about the shape of a
number, so several malformed numbers parsed and were then written back out
verbatim, producing JSON that other parsers reject:
$ echo '[2.e3]' | ./json_parse -s -
[ 2.e3 ]
RFC 8259 requires a mandatory integer part and at least one digit in both the
fraction and the exponent:
number = [ minus ] int [ frac ] [ exp ]
int = zero / ( digit1-9 *DIGIT )
frac = decimal-point 1*DIGIT
exp = e [ minus / plus ] 1*DIGIT
so "1.", "-2.", "2.e3", "2.e+3", "0.e1", "-.123", "1e" and "1e+" are all
invalid. Walk the accumulated text against that grammar instead. The leading
zero rule from the previous check is part of the same walk rather than a
separate test, and its cases keep their coverage.
Only JSON_TOKENER_STRICT is affected; the default tokener stays as lenient as
it was. Measured against JSONTestSuite (318 files): strict mode went from 44
to 37 files accepted that the suite says must be rejected, default mode stayed
at 71, and nothing that must be accepted regressed in either mode.
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.
Follows on from #953, in the same block and with the same error.
Strict mode rejected leading zeros but nothing else about the shape of a number, so several malformed numbers parsed — and since the text is written back out verbatim, json-c then emitted JSON that other parsers reject:
RFC 8259 makes the integer part mandatory and wants at least one digit in both the fraction and the exponent:
so
1.,-2.,2.e3,2.e+3,2.e-3,0.e1,-.123,1eand1e+are all invalid.The change
Walk the accumulated text against that grammar rather than testing one rule of it. The leading-zero check from #953 becomes part of the same walk instead of a separate test; its cases (
00,-00,-01,-0123,01.5, and the valid-0/0.5) keep their coverage and still pass.Only
JSON_TOKENER_STRICTis touched. The default tokener is deliberately lenient and stays exactly as it was.Measurements
Against JSONTestSuite (318 files), built before and after:
n_*wrongly acceptedn_*wrongly acceptedy_*wrongly rejectedThe default row is measured from a rebuild at the parent commit rather than inferred from the change being inside the
STRICTbranch, since silently tightening the default would be worse than the bug.ctestis 28/28. Reverting the walk back to the leading-zero-only check failstest_parse.Two notes on the tests
.5is not affected by this. It never reaches the number state — it fails earlier as an unexpected character — so I dropped the case I had written for it rather than imply this change is what rejects it.The remaining
n_*files that strict mode still accepts are almost all truncated documents (n_array_unclosed,n_string_incomplete_escapeand friends), where the tokener returnsjson_tokener_continuebecause more input could still arrive. That is the incremental API working as designed, not something this change tries to address.