Repository navigation
Fix leak in JSON.stringify when an array index can't be converted - #1784
Merged
Merged
Conversation
When JSON.stringify walks an array, it reads each element and then converts the element's index to a string for js_json_check(). If that conversion failed, e.g. for lack of memory, the element was not freed, and it kept its whole realm alive until the runtime was freed. Free the element on that path, like the object branch already does when quoting the property name fails.
IamYipi
force-pushed
the
fix/json-stringify-leak
branch
from
October 7, 2026 08:21
fbb38ae to
964d942
Compare
Contributor
Author
|
Rebased on master to resolve the conflict in The earlier Windows failures look unrelated to this change: the MinGW job failed to link |
Contributor
Author
|
Thank you for reviewing and merging this, @saghul! It's always a pleasure to contribute to QuickJS-ng. |
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.
When
JSON.stringifywalks an array, it reads each element and then converts the element's index to a string forjs_json_check(). If that conversion fails, the element is not freed:With a memory limit this happens when the limit is reached just before the index string is allocated. The leaked element holds the realm's objects through its prototype, so none of them are freed either, and
JS_FreeRuntime()stops onassert(list_empty(&rt->gc_obj_list)). ForJSON.stringify([{}]):This frees the element on that path, as the object branch already does when quoting the property name fails.
Test
json_stringify_oominapi-test.cfollowsbacktrace_oom_callsite_array: it callsJSON.stringify([{}])with the limit set 0 to 2047 bytes above the current usage, on a runtime fromnew_runtime().JSON.stringifyis called once before the loop, because it is set up on its first access. The test aborts onmaster(debug build, with and without sanitizers) and passes with this change.make test(0/117 errors),make cxxtestand the wholeapi-testpass in a Debug build and with ASan+UBSan.