From ac09584154ca6d2d3018ef214dc7e776ab5a4b08 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 8 Oct 2026 10:40:21 +0200 Subject: [PATCH] Make OOM exceptions uncatchable by JS code Fixes: https://github.com/quickjs-ng/quickjs/pull/1779 --- api-test.c | 10 ++++++++-- quickjs.c | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/api-test.c b/api-test.c index 8c7aa05ce..1e912cb29 100644 --- a/api-test.c +++ b/api-test.c @@ -1085,8 +1085,14 @@ static void large_allocation_accounting(void) JS_ComputeMemoryUsage(rt, &before); JS_SetMemoryLimit(rt, (size_t)before.malloc_size + 4 * block_size); ret = eval(ctx, "globalThis.a = [];\n" - "for (let i = 0; i < 64; i++)\n" - " a.push(new Uint8Array(1024 * 1024));"); + "for (let i = 0; i < 64; i++) {\n" + " // OOM exception should be uncatchable\n" + " try {\n" + " a.push(new Uint8Array(1024 * 1024));" + " } catch (e) {\n" + " throw 'unexpected exception';\n" + " }\n" + "}"); assert(JS_IsException(ret)); JS_SetMemoryLimit(rt, 0); JS_ComputeMemoryUsage(rt, &after); diff --git a/quickjs.c b/quickjs.c index a39b8ac1c..6b5541e28 100644 --- a/quickjs.c +++ b/quickjs.c @@ -8563,6 +8563,7 @@ JSValue JS_ThrowOutOfMemory(JSContext *ctx) if (!rt->in_out_of_memory) { rt->in_out_of_memory = true; JS_ThrowInternalError(ctx, "out of memory"); + JS_SetUncatchableError(ctx, ctx->rt->current_exception); rt->in_out_of_memory = false; } return JS_EXCEPTION; @@ -49781,7 +49782,7 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, JS_ThrowInterrupted(ctx); break; case LRE_RET_MEMORY_ERROR: - JS_ThrowInternalError(ctx, "out of memory in regexp execution"); + JS_ThrowOutOfMemory(ctx); break; case LRE_RET_BYTECODE_ERROR: JS_ThrowInternalError(ctx, "corrupted bytecode in regexp execution");