Repository navigation
Conversation
JsonObjectAppendElement() removes an existing member of the same name before appending, so a document with two members of one name loses the earlier one while it is being parsed, before the caller can look at it. JsonParse() and everything built on it (JsonParseFile(), JsonParseAnyFile(), JsonReadDataFile()) also stop at the end of the first value and ignore whatever follows. A caller that must not act on a document it only half-read, such as a loader that validates every entry, needs both to be refused. The two new functions do that: a name that an object already holds is refused with the new JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY before the earlier member is replaced, at any depth, and anything but whitespace after the value is refused with JSON_PARSE_ERROR_INVALID_END. The file variant keeps the size cap and also refuses a null byte, which would hide the rest of the file from the parser. This is opt-in: the existing functions behave exactly as before, with duplicates resolved last-wins and a valid prefix accepted. The same choice is made by Jansson as JSON_REJECT_DUPLICATES, and RFC 8259 section 4 leaves duplicate names to the implementation. Changelog: Title Ticket: none Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Automatic reviews are disabled for this repository. Configure in Code Review Settings. |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: dc3fba212b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| const char *data = StringWriterData(contents); | ||
| JsonParseError error; | ||
| if (strlen(data) != StringWriterLength(contents)) |
There was a problem hiding this comment.
Detect null bytes before FileRead discards them
For a file containing {}\0not-json, this check does not reject the hidden suffix: FileReadFromFd() passes each read buffer to WriterWriteLen(), whose StringWriterWriteLen() implementation uses strnlen() and discards the null byte and the remainder of that buffer. Consequently, strlen(data) still equals StringWriterLength(contents), and JsonParseFileStrict() successfully parses {} instead of returning JSON_PARSE_ERROR_INVALID_END. This also affects the embedded-null case already added to test_parse_file_strict(). Detect null bytes in the raw read buffers, or use a binary-preserving read path before validating the document.
Useful? React with 👍 / 👎.
FileRead() builds its Writer with strnlen(), so a null byte cuts the
text off there and the Writer's length still equals the length of the
string that is left: the check that compared the two could never fire,
and a file such as {"vars": {}}<NUL>not-json parsed as the valid
object. The function now reads through FileReadFromFd() and compares the
number of bytes it got with the size fstat() reports for the same
descriptor, so a null byte (or a file that changed while it was read) is
refused with JSON_PARSE_ERROR_INVALID_END.
Found by the unit tests in fork CI (test_parse_file_strict).
Changelog: None
Ticket: none
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Adds
JsonParseStrict()andJsonParseFileStrict(), an opt-in parse thatrefuses a duplicate object member at any depth and any non-whitespace after
the value. A duplicate is detected when the second key is read, before
JsonObjectAppendElement()could replace the first member, so no member isever dropped; the new error is
JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY.Trailing text gives the existing
JSON_PARSE_ERROR_INVALID_END. The filevariant keeps the
size_maxcap (JSON_PARSE_ERROR_TRUNCATED) and refusesa null byte, which would hide the rest of the file from the parser.
JsonParse(),JsonParseAll(),JsonParseWithLookup(),JsonParseFile(),JsonParseAnyFile()andJsonReadDataFile()are unchanged (duplicateslast-wins, valid prefix accepted); only a new
reject_duplicatesargumentis threaded through the static array/object parsers, passed as false by
the existing entry points.
Tests (
tests/unit/json_test.c): valid documents and trailing whitespace;duplicate member at top level, nested, inside an array, unquoted names;
an invalid earlier duplicate (null then string); a repeated top-level
section; trailing non-whitespace after object, array, number, bool, null
and string; file variant (valid, duplicate, trailing, null byte, size cap,
missing file, empty file); regression tests that the lenient functions
still resolve duplicates last-wins and accept a prefix. They are run by fork
CI; none were run on a Mac.
git merge-treeagainstfork/tendcf-integrationis conflict-free.
Closes #10
Prior art / docs checked: RFC 8259 section 4 (duplicate names are
unpredictable; reporting an error is a permitted implementation choice);
Jansson
JSON_REJECT_DUPLICATESandJSON_DISABLE_EOF_CHECK(opt-inrejection, whole input by default); NorthernTechHQ/libntech and
djbclark/libntech issues and PRs for duplicate keys and trailing text, none
found.
Found by review of frdminc/tendcf#41 (A2).
🤖 Generated with Claude Code