Skip to content

Check the whole RFC 8259 number grammar in strict mode - #957

Merged
hawicz merged 1 commit into
json-c:masterfrom
dngr2:fix/strict-number-grammar
Aug 17, 2026
Merged

hawicz merged 1 commit into
json-c:masterfrom
dngr2:fix/strict-number-grammar

Conversation

@dngr2

@dngr2 dngr2 commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

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:

$ echo '[2.e3]' | ./apps/json_parse -s -
[ 2.e3 ]
$ echo '[1.]'   | ./apps/json_parse -s -
[ 1. ]
$ echo '[-.123]' | ./apps/json_parse -s -
[ -.123 ]

RFC 8259 makes the integer part mandatory and wants 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, 2.e-3, 0.e1, -.123, 1e and 1e+ 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_STRICT is touched. The default tokener is deliberately lenient and stays exactly as it was.

Measurements

Against JSONTestSuite (318 files), built before and after:

before after
strict: n_* wrongly accepted 44 37
default: n_* wrongly accepted 71 71
either mode: y_* wrongly rejected 0 0

The default row is measured from a rebuild at the parent commit rather than inferred from the change being inside the STRICT branch, since silently tightening the default would be worse than the bug.

ctest is 28/28. Reverting the walk back to the leading-zero-only check fails test_parse.

Two notes on the tests

.5 is 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_escape and friends), where the tokener returns json_tokener_continue because more input could still arrive. That is the incremental API working as designed, not something this change tries to address.

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.
@hawicz
hawicz merged commit 8319071 into json-c:master Aug 17, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants