Repository navigation
Conversation
do_dump ignored dump_string's return value for object keys, on both the
JSON_SORT_KEYS and the unsorted path. When the output buffer could not
grow for a long key, the key was dropped, but the ": " and the value
after it were small enough to fit, so json_dumps returned malformed
JSON such as {": 1, "a": 2} instead of NULL.
Check dump_string's result at both sites, as is already done for string
values. Two things now fail the dump instead of producing malformed
output: a key that cannot be written (out of memory, or a dump callback
or file write that returns an error), and a key that is not valid UTF-8,
which only the _nocheck setters can create.
Tests: an object with a 4 KB key dumped through an allocator that
refuses large blocks, and an object with an invalid UTF-8 key, each
sorted and unsorted.
Fixes akheron#655.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fail json_dumps when an object key cannot be written
Fixes #655.
do_dumpignoresdump_string's return value for object keys, on both theJSON_SORT_KEYSand the unsorted path. When the output buffer cannot grow for a long key, the key is dropped, but the": "and the value after it are small enough to fit, sojson_dumpsreturns malformed JSON such as{": 1, "a": 2}instead ofNULL.This checks
dump_string's result at both sites, as is already done for string values.Behaviour change: two cases now make
json_dumps/json_dump_callback/json_dumpffail instead of producing malformed output:_nochecksetters can create one. On master{"ab\xff": 1, "x": 2}dumps as{": 1, "x": 2}; it now fails, the same as an invalid string value.json_dumpbis unaffected (its callback never fails). The sorted path still frees its key array on the new early return.Tests in
test/suites/api/test_dump.c:dump_key_out_of_memorydumps an object with a 4 KB key through an allocator that refuses blocks over 1 KB, anddump_key_invalid_utf8dumps one with an invalid UTF-8 key, each sorted and unsorted. Both fail on master and pass with the fix. The full suite passes (215 tests), also under ASan/UBSan.