Skip to content

Fix leak in JSON.stringify when an array index can't be converted - #1784

Merged
saghul merged 1 commit into
quickjs-ng:masterfrom
IamYipi:fix/json-stringify-leak
Oct 7, 2026
Merged

saghul merged 1 commit into
quickjs-ng:masterfrom
IamYipi:fix/json-stringify-leak

Conversation

@IamYipi

@IamYipi IamYipi commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

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 fails, the element is not freed:

v = JS_GetPropertyInt64(ctx, val, i);
if (JS_IsException(v))
    goto exception;
/* XXX: could do this string conversion only when needed */
prop = JS_ToStringFree(ctx, js_int64(i));
if (JS_IsException(prop))
    goto exception;          /* v 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 on assert(list_empty(&rt->gc_obj_list)). For JSON.stringify([{}]):

Object leaks:
       ADDRESS REFS SHRF          PROTO      CLASS PROPS
0x55bcb76e0250    1   0* 0x55bcb76e55e0     Object {  }
api-test: quickjs.c:2699: JS_FreeRuntime: Assertion `list_empty(&rt->gc_obj_list)' failed.

This frees the element on that path, as the object branch already does when quoting the property name fails.

Test

json_stringify_oom in api-test.c follows backtrace_oom_callsite_array: it calls JSON.stringify([{}]) with the limit set 0 to 2047 bytes above the current usage, on a runtime from new_runtime(). JSON.stringify is called once before the loop, because it is set up on its first access. The test aborts on master (debug build, with and without sanitizers) and passes with this change.

make test (0/117 errors), make cxxtest and the whole api-test pass in a Debug build and with ASan+UBSan.

@saghul saghul left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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
IamYipi force-pushed the fix/json-stringify-leak branch from fbb38ae to 964d942 Compare October 7, 2026 08:21
@IamYipi

IamYipi commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on master to resolve the conflict in api-test.c, where master gained new_context_low_memory() (#1781) next to this test. make test (Debug, and ASan+UBSan), api-test in both builds and make cxxtest pass.

The earlier Windows failures look unrelated to this change: the MinGW job failed to link qjs.exe with undefined reference to mi_malloc_usable_size in qjs.c, and the ARM64 MSVC jobs failed in CMake configuration because the "Visual Studio 17" generator was not found on the runner.

@saghul
saghul merged commit 293ba08 into quickjs-ng:master Oct 7, 2026
128 checks passed
@IamYipi

IamYipi commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for reviewing and merging this, @saghul! It's always a pleasure to contribute to QuickJS-ng.

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