Skip to content

Accept Infinity and NaN at the end of the buffer - #130

Merged
sheredom merged 1 commit into
sheredom:mainfrom
youdie006:inf-nan-end-of-buffer
Sep 28, 2026
Merged

sheredom merged 1 commit into
sheredom:mainfrom
youdie006:inf-nan-end-of-buffer

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

With json_parse_flags_allow_inf_and_nan, Infinity, -Infinity, NaN and -NaN fail with json_parse_error_invalid_number_format when they are the last bytes of the input, while the same value followed by whitespace, or 1 at the end, parses:

input (exact size) flags master this PR
-Infinity json5 error 5 ok
foo = NaN simplified + inf_and_nan error 5 ok
foo = NaN\n simplified + inf_and_nan ok ok

The keyword checks in json_get_number_size (json.h:1245, :1263) and json_parse_number (:1980, :1990) use offset + strlen < size; the dispatcher in json_get_value_size (:1492, :1497) and the true/false/null checks use <=. This changes the four sites to <=; the last byte read is still offset + strlen - 1. Both passes must change together, otherwise sizing and parsing disagree.

Tests are added after rejects_Infinity_decimal_continuation, plus a truncated -Infinit (8-byte array, no terminator) after nan_overrun that must still fail. The suite passes (1692 tests) in a gcc Debug build with ASan and UBSan; on master the two new end-of-buffer tests fail in every suite. I did not run clang, tcc, MSVC or macOS.

Written with AI assistance (Claude); I have reviewed the change.

json_get_number_size and json_parse_number checked
offset + strlen < size before matching Infinity and NaN, so a keyword
ending exactly at the end of the input was rejected. The dispatcher in
json_get_value_size already uses <=, as do true, false and null.
@sheredom

Copy link
Copy Markdown
Owner

Hah! Great find.

@sheredom
sheredom merged commit 8d14da9 into sheredom:main Sep 28, 2026
24 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