From f1253f2ab53d3adbe918fe1d68710269f0c2335e Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Fri, 3 Oct 2025 12:00:26 +0200 Subject: [PATCH] Improve error handling in Promise.withResolvers (bnoordhuis) --- quickjs.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/quickjs.c b/quickjs.c index f0f155d..6b2cc59 100644 --- a/quickjs.c +++ b/quickjs.c @@ -51667,16 +51667,29 @@ static JSValue js_promise_withResolvers(JSContext *ctx, if (JS_IsException(result_promise)) return result_promise; obj = JS_NewObject(ctx); - if (JS_IsException(obj)) { - JS_FreeValue(ctx, resolving_funcs[0]); - JS_FreeValue(ctx, resolving_funcs[1]); - JS_FreeValue(ctx, result_promise); - return JS_EXCEPTION; + if (JS_IsException(obj)) + goto exception; + if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_promise, result_promise, + JS_PROP_C_W_E) < 0) { + goto exception; + } + result_promise = JS_UNDEFINED; + if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_resolve, resolving_funcs[0], + JS_PROP_C_W_E) < 0) { + goto exception; + } + resolving_funcs[0] = JS_UNDEFINED; + if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_reject, resolving_funcs[1], + JS_PROP_C_W_E) < 0) { + goto exception; } - JS_DefinePropertyValue(ctx, obj, JS_ATOM_promise, result_promise, JS_PROP_C_W_E); - JS_DefinePropertyValue(ctx, obj, JS_ATOM_resolve, resolving_funcs[0], JS_PROP_C_W_E); - JS_DefinePropertyValue(ctx, obj, JS_ATOM_reject, resolving_funcs[1], JS_PROP_C_W_E); return obj; +exception: + JS_FreeValue(ctx, resolving_funcs[0]); + JS_FreeValue(ctx, resolving_funcs[1]); + JS_FreeValue(ctx, result_promise); + JS_FreeValue(ctx, obj); + return JS_EXCEPTION; } static JSValue js_promise_try(JSContext *ctx, JSValueConst this_val,