From b50e8fc08e13bb8692d3ae2a39506c67fe58f3ef Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 13:22:40 +0100 Subject: [PATCH 01/16] don't append in place to strings that are atoms (bnoordhuis) --- quickjs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 4b9ba1b..55ffcbc 100644 --- a/quickjs.c +++ b/quickjs.c @@ -4793,7 +4793,7 @@ static BOOL JS_ConcatStringInPlace(JSContext *ctx, JSString *p1, JSValueConst op if (p2->len == 0) return TRUE; - if (js_rc(p1)->ref_count != 1) + if (js_rc(p1)->ref_count != 1 || p1->atom_type != 0) return FALSE; size1 = js_malloc_usable_size(ctx, p1); if (p1->is_wide_char) { -- 2.47.3 From 2f97a49dbc3816b9e8a931c91cdb30a56bb64e44 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 14:13:46 +0100 Subject: [PATCH 02/16] don't take value of dropped iterator items --- quickjs.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 55ffcbc..bb092d0 100644 --- a/quickjs.c +++ b/quickjs.c @@ -47194,9 +47194,18 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, JSValue item; while (it->count > 0) { it->count--; - item = JS_IteratorNext(ctx, it->obj, it->next, pdone); + item = JS_IteratorNext2(ctx, it->obj, it->next, 0, NULL, pdone); if (JS_IsException(item)) goto fail_no_close; + if (*pdone == 2) { + JSValue done_val; + done_val = JS_GetProperty(ctx, item, JS_ATOM_done); + if (JS_IsException(done_val)) { + JS_FreeValue(ctx, item); + goto fail_no_close; + } + *pdone = JS_ToBoolFree(ctx, done_val); + } JS_FreeValue(ctx, item); if (*pdone) { ret = JS_UNDEFINED; -- 2.47.3 From fbc494bd89669015f0f668559c51ec290636643f Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 14:27:16 +0100 Subject: [PATCH 03/16] fixed Iterator.from wrapper's next() method so that it returns the object as-is --- quickjs.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/quickjs.c b/quickjs.c index bb092d0..9983a62 100644 --- a/quickjs.c +++ b/quickjs.c @@ -16964,9 +16964,10 @@ JSValue JS_GetIterator(JSContext *ctx, JSValueConst obj, BOOL is_async) } /* return *pdone = 2 if the iterator object is not parsed */ -static JSValue JS_IteratorNext2(JSContext *ctx, JSValueConst enum_obj, +static JSValue JS_IteratorNext3(JSContext *ctx, JSValueConst enum_obj, JSValueConst method, - int argc, JSValueConst *argv, int *pdone) + int argc, JSValueConst *argv, int *pdone, + BOOL check_object) { JSValue obj; @@ -16992,7 +16993,7 @@ static JSValue JS_IteratorNext2(JSContext *ctx, JSValueConst enum_obj, obj = JS_Call(ctx, method, enum_obj, argc, argv); if (JS_IsException(obj)) goto fail; - if (!JS_IsObject(obj)) { + if (check_object && !JS_IsObject(obj)) { JS_FreeValue(ctx, obj); JS_ThrowTypeError(ctx, "iterator must return an object"); goto fail; @@ -17004,6 +17005,13 @@ static JSValue JS_IteratorNext2(JSContext *ctx, JSValueConst enum_obj, return JS_EXCEPTION; } +static JSValue JS_IteratorNext2(JSContext *ctx, JSValueConst enum_obj, + JSValueConst method, + int argc, JSValueConst *argv, int *pdone) +{ + return JS_IteratorNext3(ctx, enum_obj, method, argc, argv, pdone, TRUE); +} + /* Note: always return JS_UNDEFINED when *pdone = TRUE. */ JSValue JS_IteratorNext(JSContext *ctx, JSValueConst enum_obj, JSValueConst method, BOOL *pdone) @@ -45519,7 +45527,7 @@ static JSValue js_iterator_wrap_next(JSContext *ctx, JSValueConst this_val, if (!it) return JS_EXCEPTION; if (magic == GEN_MAGIC_NEXT) { - return JS_IteratorNext(ctx, it->wrapped_iter, it->wrapped_next, pdone); + return JS_IteratorNext3(ctx, it->wrapped_iter, it->wrapped_next, 0, NULL, pdone, FALSE); } else { method = JS_GetProperty(ctx, it->wrapped_iter, JS_ATOM_return); if (JS_IsException(method)) @@ -45528,8 +45536,8 @@ static JSValue js_iterator_wrap_next(JSContext *ctx, JSValueConst this_val, *pdone = TRUE; return JS_UNDEFINED; } - ret = JS_IteratorNext2(ctx, it->wrapped_iter, method, 0, NULL, pdone); - JS_FreeValue(ctx, method); + ret = JS_CallFree(ctx, method, it->wrapped_iter, 0, NULL); + *pdone = 2; return ret; } } -- 2.47.3 From d28175ab5f7519bce34bc82e2e2a7143d4e78990 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 14:32:04 +0100 Subject: [PATCH 04/16] Check for stack overflow in JS_IteratorNext fast path (josefguenther) --- quickjs.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/quickjs.c b/quickjs.c index 9983a62..535ebcc 100644 --- a/quickjs.c +++ b/quickjs.c @@ -16980,6 +16980,10 @@ static JSValue JS_IteratorNext3(JSContext *ctx, JSValueConst enum_obj, JSCFunctionType func; JSValueConst args[1]; + if (js_check_stack_overflow(ctx->rt, 0)) { + JS_ThrowStackOverflow(ctx); + goto fail; + } /* in case the function expects one argument */ if (argc == 0) { args[0] = JS_UNDEFINED; -- 2.47.3 From 557d8437d67f3c7b7ea391ded6b26900750fdbae Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 14:37:44 +0100 Subject: [PATCH 05/16] Check the stack when instanceof follows a bound function --- quickjs.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/quickjs.c b/quickjs.c index 535ebcc..dd83cc9 100644 --- a/quickjs.c +++ b/quickjs.c @@ -8206,6 +8206,10 @@ int JS_OrdinaryIsInstanceOf(JSContext *ctx, JSValueConst val, p = JS_VALUE_GET_OBJ(obj); if (p->class_id == JS_CLASS_BOUND_FUNCTION) { JSBoundFunction *s = p->u.bound_function; + if (js_check_stack_overflow(ctx->rt, 0)) { + JS_ThrowStackOverflow(ctx); + return -1; + } return JS_IsInstanceOf(ctx, val, s->func_obj); } -- 2.47.3 From 76feca5f0433bf1eef2653721a5b96e1289aecbd Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 14:44:42 +0100 Subject: [PATCH 06/16] Poll for interrupts on a taken OP_with_* branch --- quickjs.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/quickjs.c b/quickjs.c index dd83cc9..fabeac6 100644 --- a/quickjs.c +++ b/quickjs.c @@ -21213,6 +21213,8 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj, break; } pc += diff - 5; + if (unlikely(js_poll_interrupts(ctx))) + goto exception; } else { no_with: /* if not jumping, drop the object argument */ -- 2.47.3 From b833931b9c4feab9c26f8e7b7245d55ffdb28911 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 14:49:09 +0100 Subject: [PATCH 07/16] Drop the new prototype's reference when the shape cannot be made writable --- quickjs.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index fabeac6..feec990 100644 --- a/quickjs.c +++ b/quickjs.c @@ -8112,8 +8112,11 @@ static int JS_SetPrototypeInternal(JSContext *ctx, JSValueConst obj, JS_DupValue(ctx, proto_val); } - if (js_shape_prepare_update(ctx, p, NULL)) + if (js_shape_prepare_update(ctx, p, NULL)) { + if (proto) + JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, proto)); return -1; + } sh = p->shape; if (sh->proto) JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, sh->proto)); -- 2.47.3 From dfe36a811443c6d4e7ea6029c83c1e3e82c09c7c Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 15:01:16 +0100 Subject: [PATCH 08/16] Release the element functions when Promise.allSettled() or any() fails --- quickjs.c | 183 +++++++++++++++++++++++++++--------------------------- 1 file changed, 93 insertions(+), 90 deletions(-) diff --git a/quickjs.c b/quickjs.c index feec990..cd8fe1b 100644 --- a/quickjs.c +++ b/quickjs.c @@ -57525,113 +57525,116 @@ static JSValue js_promise_all(JSContext *ctx, JSValueConst this_val, check_function(ctx, promise_resolve)) goto fail_reject; iter = JS_GetIterator(ctx, argv[0], FALSE); - if (JS_IsException(iter)) { - JSValue error; - fail_reject: - error = JS_GetException(ctx); - ret = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED, 1, - (JSValueConst *)&error); - JS_FreeValue(ctx, error); - if (JS_IsException(ret)) - goto fail; - JS_FreeValue(ctx, ret); - } else { - next_method = JS_GetProperty(ctx, iter, JS_ATOM_next); - if (JS_IsException(next_method)) - goto fail_reject; - values = JS_NewArray(ctx); - if (JS_IsException(values)) - goto fail_reject; - resolve_element_env = JS_NewArray(ctx); - if (JS_IsException(resolve_element_env)) - goto fail_reject; - /* remainingElementsCount field */ - if (JS_DefinePropertyValueUint32(ctx, resolve_element_env, 0, - JS_NewInt32(ctx, 1), - JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE | JS_PROP_WRITABLE) < 0) + if (JS_IsException(iter)) + goto fail_reject; + next_method = JS_GetProperty(ctx, iter, JS_ATOM_next); + if (JS_IsException(next_method)) + goto fail_reject; + values = JS_NewArray(ctx); + if (JS_IsException(values)) + goto fail_reject; + resolve_element_env = JS_NewArray(ctx); + if (JS_IsException(resolve_element_env)) + goto fail_reject; + /* remainingElementsCount field */ + if (JS_DefinePropertyValueUint32(ctx, resolve_element_env, 0, + JS_NewInt32(ctx, 1), + JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE | JS_PROP_WRITABLE) < 0) + goto fail_reject; + + index = 0; + for(;;) { + /* XXX: conformance: should close the iterator if error on 'done' + access, but not on 'value' access */ + item = JS_IteratorNext(ctx, iter, next_method, &done); + if (JS_IsException(item)) goto fail_reject; + if (done) + break; + next_promise = JS_Call(ctx, promise_resolve, + this_val, 1, (JSValueConst *)&item); + JS_FreeValue(ctx, item); + if (JS_IsException(next_promise)) + goto fail_reject1; + resolve_element_data[0] = JS_NewBool(ctx, FALSE); + resolve_element_data[1] = (JSValueConst)JS_NewInt32(ctx, index); + resolve_element_data[2] = values; + resolve_element_data[3] = resolving_funcs[is_promise_any]; + resolve_element_data[4] = resolve_element_env; + resolve_element = + JS_NewCFunctionData(ctx, js_promise_all_resolve_element, 1, + magic, 5, resolve_element_data); + if (JS_IsException(resolve_element)) { + JS_FreeValue(ctx, next_promise); + goto fail_reject1; + } - index = 0; - for(;;) { - /* XXX: conformance: should close the iterator if error on 'done' - access, but not on 'value' access */ - item = JS_IteratorNext(ctx, iter, next_method, &done); - if (JS_IsException(item)) - goto fail_reject; - if (done) - break; - next_promise = JS_Call(ctx, promise_resolve, - this_val, 1, (JSValueConst *)&item); - JS_FreeValue(ctx, item); - if (JS_IsException(next_promise)) { - fail_reject1: - JS_IteratorClose(ctx, iter, TRUE); - goto fail_reject; - } - resolve_element_data[0] = JS_NewBool(ctx, FALSE); - resolve_element_data[1] = (JSValueConst)JS_NewInt32(ctx, index); - resolve_element_data[2] = values; - resolve_element_data[3] = resolving_funcs[is_promise_any]; - resolve_element_data[4] = resolve_element_env; - resolve_element = + if (magic == PROMISE_MAGIC_allSettled) { + reject_element = JS_NewCFunctionData(ctx, js_promise_all_resolve_element, 1, - magic, 5, resolve_element_data); - if (JS_IsException(resolve_element)) { + magic | 4, 5, resolve_element_data); + if (JS_IsException(reject_element)) { JS_FreeValue(ctx, next_promise); + JS_FreeValue(ctx, resolve_element); goto fail_reject1; } - - if (magic == PROMISE_MAGIC_allSettled) { - reject_element = - JS_NewCFunctionData(ctx, js_promise_all_resolve_element, 1, - magic | 4, 5, resolve_element_data); - if (JS_IsException(reject_element)) { - JS_FreeValue(ctx, next_promise); - goto fail_reject1; - } - } else if (magic == PROMISE_MAGIC_any) { - if (JS_DefinePropertyValueUint32(ctx, values, index, - JS_UNDEFINED, JS_PROP_C_W_E) < 0) - goto fail_reject1; - reject_element = resolve_element; - resolve_element = JS_DupValue(ctx, resolving_funcs[0]); - } else { - reject_element = JS_DupValue(ctx, resolving_funcs[1]); - } - - if (remainingElementsCount_add(ctx, resolve_element_env, 1) < 0) { + } else if (magic == PROMISE_MAGIC_any) { + if (JS_DefinePropertyValueUint32(ctx, values, index, + JS_UNDEFINED, JS_PROP_C_W_E) < 0) { JS_FreeValue(ctx, next_promise); JS_FreeValue(ctx, resolve_element); - JS_FreeValue(ctx, reject_element); goto fail_reject1; } + reject_element = resolve_element; + resolve_element = JS_DupValue(ctx, resolving_funcs[0]); + } else { + reject_element = JS_DupValue(ctx, resolving_funcs[1]); + } - then_args[0] = resolve_element; - then_args[1] = reject_element; - ret = JS_InvokeFree(ctx, next_promise, JS_ATOM_then, 2, then_args); + if (remainingElementsCount_add(ctx, resolve_element_env, 1) < 0) { + JS_FreeValue(ctx, next_promise); JS_FreeValue(ctx, resolve_element); JS_FreeValue(ctx, reject_element); - if (check_exception_free(ctx, ret)) - goto fail_reject1; - index++; + goto fail_reject1; } - is_zero = remainingElementsCount_add(ctx, resolve_element_env, -1); - if (is_zero < 0) + then_args[0] = resolve_element; + then_args[1] = reject_element; + ret = JS_InvokeFree(ctx, next_promise, JS_ATOM_then, 2, then_args); + JS_FreeValue(ctx, resolve_element); + JS_FreeValue(ctx, reject_element); + if (check_exception_free(ctx, ret)) { + fail_reject1: + JS_IteratorClose(ctx, iter, TRUE); goto fail_reject; - if (is_zero) { - if (magic == PROMISE_MAGIC_any) { - JSValue error; - error = js_aggregate_error_constructor(ctx, values); - if (JS_IsException(error)) - goto fail_reject; - JS_FreeValue(ctx, values); - values = error; - } - ret = JS_Call(ctx, resolving_funcs[is_promise_any], JS_UNDEFINED, - 1, (JSValueConst *)&values); - if (check_exception_free(ctx, ret)) + } + index++; + } + + is_zero = remainingElementsCount_add(ctx, resolve_element_env, -1); + if (is_zero < 0) + goto fail_reject; + if (is_zero) { + if (magic == PROMISE_MAGIC_any) { + JSValue error; + error = js_aggregate_error_constructor(ctx, values); + if (JS_IsException(error)) goto fail_reject; + JS_FreeValue(ctx, values); + values = error; + } + ret = JS_Call(ctx, resolving_funcs[is_promise_any], JS_UNDEFINED, + 1, (JSValueConst *)&values); + if (check_exception_free(ctx, ret)) { + JSValue error; + fail_reject: + error = JS_GetException(ctx); + ret = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED, 1, + (JSValueConst *)&error); + JS_FreeValue(ctx, error); + if (JS_IsException(ret)) + goto fail; + JS_FreeValue(ctx, ret); } } done: -- 2.47.3 From 1eba3837f70a35a32cf3a9a843f9bfb366bc6f6b Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 15:10:43 +0100 Subject: [PATCH 09/16] Fix the stack depth of an optional call through a with scope --- quickjs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index cd8fe1b..9e90533 100644 --- a/quickjs.c +++ b/quickjs.c @@ -27892,6 +27892,7 @@ static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags) int scope; name = get_u32(fd->byte_code.buf + fd->last_opcode_pos + 1); scope = get_u16(fd->byte_code.buf + fd->last_opcode_pos + 5); + drop_count = 1; if (name == JS_ATOM_eval && call_type == FUNC_CALL_NORMAL && !has_optional_chain) { /* direct 'eval' */ opcode = OP_eval; @@ -27907,9 +27908,9 @@ static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags) if (has_with_scope(fd, scope)) { opcode = OP_scope_get_ref; fd->byte_code.buf[fd->last_opcode_pos] = opcode; + drop_count = 2; } } - drop_count = 1; } break; case OP_get_super_value: -- 2.47.3 From c9d81f5173cfea0e6bf1a962697756815ca70ccd Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 16:56:06 +0100 Subject: [PATCH 10/16] reject destructuring patterns without ':' after a property name --- quickjs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 9e90533..7d23daf 100644 --- a/quickjs.c +++ b/quickjs.c @@ -27123,7 +27123,7 @@ static int js_parse_destructuring_element(JSParseState *s, int tok, int is_arg, return -1; var_name = JS_ATOM_NULL; if (prop_type == PROP_TYPE_IDENT) { - if (next_token(s)) + if (js_parse_expect(s, ':')) goto prop_error; if ((s->token.val == '[' || s->token.val == '{') && ((tok1 = js_parse_skip_parens_token(s, &skip_bits, FALSE)) == ',' || -- 2.47.3 From c4b449ce4b119bd43b48898a8b0f69f2c6522d82 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 17:02:32 +0100 Subject: [PATCH 11/16] fixed array literal parsing (gengjiawen) --- quickjs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 7d23daf..1544e4f 100644 --- a/quickjs.c +++ b/quickjs.c @@ -26498,7 +26498,9 @@ static __exception int js_parse_array_literal(JSParseState *s) if (s->token.val == ',') { if (next_token(s)) return -1; - } + } else + if (s->token.val != ']') + goto done; } if (s->token.val == ']') { if (need_length) { -- 2.47.3 From 7e59fe807dc22177183efac1bf4a83f7f7b759aa Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 21 Nov 2026 17:07:34 +0100 Subject: [PATCH 12/16] Fix Function.prototype.toString for a revoked proxy (bardrop) --- quickjs.c | 5 ++++- tests/test_builtin.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 1544e4f..7d48a7d 100644 --- a/quickjs.c +++ b/quickjs.c @@ -42613,7 +42613,10 @@ static JSValue js_function_toString(JSContext *ctx, JSValueConst this_val, break; } suff = "() {\n [native code]\n}"; - name = JS_GetProperty(ctx, this_val, JS_ATOM_name); + if (p->class_id == JS_CLASS_PROXY) + name = JS_UNDEFINED; + else + name = JS_GetProperty(ctx, this_val, JS_ATOM_name); if (JS_IsUndefined(name)) name = JS_AtomToString(ctx, JS_ATOM_empty_string); return JS_ConcatString3(ctx, pref, name, suff); diff --git a/tests/test_builtin.js b/tests/test_builtin.js index 7fcc4bd..9c647d1 100644 --- a/tests/test_builtin.js +++ b/tests/test_builtin.js @@ -119,6 +119,20 @@ function test_function() g = constructor1.bind(null, 1); r = new g(); assert(r.x, 1); + + // a callable proxy stays callable after revoke, so toString works + var native = "function () {\n [native code]\n}"; + g = Proxy.revocable(function f() {}, {}); + g.revoke(); + assert(Function.prototype.toString.call(g.proxy), native); + // and it does not read "name" through the proxy + var keys = []; + g = new Proxy(function f() {}, { + get(t, k) { keys.push(k); return Reflect.get(t, k); } + }); + assert(Function.prototype.toString.call(g), native); + assert(keys.length, 0); + assert_throws(TypeError, () => Function.prototype.toString.call(new Proxy({}, {}))); } function test() -- 2.47.3 From 1eb38629d6544e532ce929bc1617a9d1123f3969 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Mon, 23 Nov 2026 12:08:22 +0100 Subject: [PATCH 13/16] nan boxing: faster NaN detection in __JS_NewFloat64 (Julien Olivier, #616) --- quickjs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickjs.h b/quickjs.h index e3fc629..9778316 100644 --- a/quickjs.h +++ b/quickjs.h @@ -191,7 +191,7 @@ static inline JSValue __JS_NewFloat64(JSContext *ctx, double d) JSValue v; u.d = d; /* normalize NaN */ - if (js_unlikely((u.u64 & 0x7fffffffffffffff) > 0x7ff0000000000000)) + if (js_unlikely(d != d)) v = JS_NAN; else v = u.u64 - JS_FLOAT64_ADDEND; -- 2.47.3 From 0a0f81743db048400ef6c9a2faf53245d2b16b1e Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Mon, 23 Nov 2026 13:57:27 +0100 Subject: [PATCH 14/16] fixed Math.sumPrecise --- quickjs.c | 2 +- tests/test_builtin.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 7d48a7d..a2e0b7f 100644 --- a/quickjs.c +++ b/quickjs.c @@ -49962,7 +49962,7 @@ typedef enum { /* we add one extra limb to avoid having to test for overflows during the sum */ #define SUM_PRECISE_ACC_LEN 39 -#define SUM_PRECISE_COUNTER_INIT 250 +#define SUM_PRECISE_COUNTER_INIT 126 typedef struct { SumPreciseStateEnum state; diff --git a/tests/test_builtin.js b/tests/test_builtin.js index 9c647d1..64c21fc 100644 --- a/tests/test_builtin.js +++ b/tests/test_builtin.js @@ -421,6 +421,7 @@ function test_math() assert(Math.hypot(3, 4), 5); assert(Math.abs(Math.hypot(3, 4, 5) - 7.0710678118654755) <= 1e-15); assert(Math.sumPrecise([1,Number.EPSILON/2,Number.MIN_VALUE]), 1.0000000000000002); + assert(Math.sumPrecise(new Array(300).fill(Math.pow(2,53)-1)), 2702159776422297000); } function test_number() -- 2.47.3 From a6da4a30d3db00094781d963c8248e3ee3a6726c Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Mon, 23 Nov 2026 14:21:34 +0100 Subject: [PATCH 15/16] limit the number of lexical scopes (#610) --- quickjs.c | 58 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/quickjs.c b/quickjs.c index a2e0b7f..81a424c 100644 --- a/quickjs.c +++ b/quickjs.c @@ -214,6 +214,7 @@ typedef enum JSErrorEnum { ARG_SCOPE_END values are reserved. */ #define JS_MAX_LOCAL_VARS 65534 #define JS_STACK_SIZE_MAX 65534 +#define JS_MAX_SCOPES 65533 #define JS_STRING_LEN_MAX ((1 << 30) - 1) /* strings <= this length are not concatenated using ropes. if too @@ -24759,12 +24760,16 @@ static int find_lexical_decl(JSContext *ctx, JSFunctionDef *fd, JSAtom name, return -1; } -static int push_scope(JSParseState *s) { +static int push_scope(JSParseState *s) +{ if (s->cur_func) { JSFunctionDef *fd = s->cur_func; int scope = fd->scope_count; - /* XXX: should check for scope overflow */ - if ((fd->scope_count + 1) > fd->scope_size) { + if (scope >= JS_MAX_SCOPES) { + JS_ThrowInternalError(s->ctx, "too many scopes"); + return -1; + } + if ((scope + 1) > fd->scope_size) { int new_size; size_t slack; JSVarScope *new_buf; @@ -25770,6 +25775,7 @@ static JSFunctionDef *js_new_function_def(JSContext *ctx, const char *filename, const uint8_t *source_ptr, GetLineColCache *get_line_col_cache); +static void js_free_function_def(JSContext *ctx, JSFunctionDef *fd); static void emit_return(JSParseState *s, BOOL hasval); static __exception int js_parse_left_hand_side_expr(JSParseState *s) @@ -25797,7 +25803,12 @@ static __exception int js_parse_class_default_ctor(JSParseState *s, fd->has_this_binding = TRUE; fd->new_target_allowed = TRUE; - push_scope(s); /* enter body scope */ + /* enter body scope */ + if (push_scope(s) < 0) { + js_free_function_def(s->ctx, fd); + return -1; + } + fd->body_scope = fd->scope_level; if (has_super) { fd->is_derived_class_constructor = TRUE; @@ -25982,7 +25993,8 @@ static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr, class_var_name = JS_DupAtom(ctx, class_var_name); } - push_scope(s); + if (push_scope(s) < 0) + goto fail; if (s->token.val == TOK_EXTENDS) { class_flags = JS_DEFINE_CLASS_HAS_HERITAGE; @@ -26005,7 +26017,8 @@ static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr, goto fail; /* this scope contains the private fields */ - push_scope(s); + if (push_scope(s) < 0) + goto fail; emit_op(s, OP_push_const); ctor_cpool_offset = fd->byte_code.size; @@ -26069,7 +26082,8 @@ static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr, goto fail; } // stack is now: fclosure - push_scope(s); + if (push_scope(s) < 0) + goto fail; emit_op(s, OP_scope_get_var); emit_atom(s, JS_ATOM_this); emit_u16(s, 0); @@ -29296,7 +29310,8 @@ static __exception int js_parse_block(JSParseState *s) return -1; if (s->token.val != '}') { BlockEnv using_be; - push_scope(s); + if (push_scope(s) < 0) + return -1; /* new BlockEnv for potential 'using' declarations */ push_break_entry(s->cur_func, &using_be, JS_ATOM_NULL, -1, -1, 0); @@ -29596,7 +29611,8 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name, /* create scope for the lexical variables declared in the enumeration expressions. XXX: Not completely correct because of weird capturing semantics in `for (i of o) a.push(function(){return i})` */ - push_scope(s); + if (push_scope(s) < 0) + return -1; /* local for_in scope starts here so individual elements can be closed in statement. */ @@ -30007,7 +30023,8 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, if (next_token(s)) goto fail; /* create a new scope for `let f;if(1) function f(){}` */ - push_scope(s); + if (push_scope(s) < 0) + goto fail; set_eval_ret_undefined(s); if (js_parse_expr_paren(s)) goto fail; @@ -30143,7 +30160,8 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, /* create scope for the lexical variables declared in the initial, test and increment expressions */ - push_scope(s); + if (push_scope(s) < 0) + goto fail; label_cont = new_label(s); label_break = new_label(s); @@ -30299,7 +30317,8 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, if (js_parse_expr_paren(s)) goto fail; - push_scope(s); + if (push_scope(s) < 0) + goto fail; label_break = new_label(s); push_break_entry(s->cur_func, &break_entry, label_name, label_break, -1, 1); @@ -30424,7 +30443,8 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, if (next_token(s)) goto fail; - push_scope(s); /* catch variable */ + if (push_scope(s) < 0) /* catch variable */ + goto fail; emit_label(s, label_catch); if (s->token.val == '{') { @@ -30460,7 +30480,8 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, /* XXX: should keep the address to nop it out if there is no finally block */ emit_goto(s, OP_catch, label_catch2); - push_scope(s); /* catch block */ + if (push_scope(s) < 0) /* catch block */ + goto fail; push_break_entry(s->cur_func, &block_env, JS_ATOM_NULL, -1, -1, 1); block_env.label_finally = label_finally; @@ -30558,7 +30579,8 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, if (js_parse_expr_paren(s)) goto fail; - push_scope(s); + if (push_scope(s) < 0) + goto fail; with_idx = define_var(s, s->cur_func, JS_ATOM__with_, JS_VAR_DEF_WITH); if (with_idx < 0) @@ -38090,7 +38112,8 @@ static __exception int js_parse_function_decl2(JSParseState *s, /* in generators, yield expression is forbidden during the parsing of the arguments */ fd->in_function_body = TRUE; - push_scope(s); /* enter body scope */ + if (push_scope(s) < 0) /* enter body scope */ + goto fail; fd->body_scope = fd->scope_level; if (s->token.val == TOK_ARROW && func_type == JS_PARSE_FUNC_ARROW) { @@ -38486,7 +38509,8 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj, s->is_module = (m != NULL); s->allow_html_comments = !s->is_module; - push_scope(s); /* body scope */ + if (push_scope(s) < 0) /* body scope */ + goto fail; fd->body_scope = fd->scope_level; err = js_parse_program(s); -- 2.47.3 From 25197744030a9659b5a6e6f861ae78981b5fce07 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Mon, 23 Nov 2026 18:26:27 +0100 Subject: [PATCH 16/16] added overflow checks for most array allocations and reallocations (#536, #535) --- quickjs.c | 342 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 190 insertions(+), 152 deletions(-) diff --git a/quickjs.c b/quickjs.c index 81a424c..c783699 100644 --- a/quickjs.c +++ b/quickjs.c @@ -1883,6 +1883,38 @@ void *js_mallocz(JSContext *ctx, size_t size) return ptr; } +/* Allocate 'nmemb * elem_size' bytes. Throw out of memory in case of + error */ +static void *js_malloc_array(JSContext *ctx, size_t nmemb, size_t elem_size) +{ + void *ptr; + if (elem_size != 0 && nmemb >= ((SIZE_MAX - 4096) / elem_size)) + goto fail; + ptr = js_malloc_rt(ctx->rt, nmemb * elem_size); + if (unlikely(!ptr)) { + fail: + JS_ThrowOutOfMemory(ctx); + return NULL; + } + return ptr; +} + +/* Allocate 'nmemb * elem_size' bytes. Throw out of memory in case of + error */ +static void *js_mallocz_array(JSContext *ctx, size_t nmemb, size_t elem_size) +{ + void *ptr; + if (elem_size != 0 && nmemb >= ((SIZE_MAX - 4096) / elem_size)) + goto fail; + ptr = js_mallocz_rt(ctx->rt, nmemb * elem_size); + if (unlikely(!ptr)) { + fail: + JS_ThrowOutOfMemory(ctx); + return NULL; + } + return ptr; +} + void js_free(JSContext *ctx, void *ptr) { js_free_rt(ctx->rt, ptr); @@ -1900,22 +1932,32 @@ void *js_realloc(JSContext *ctx, void *ptr, size_t size) return ret; } -/* store extra allocated size in *pslack if successful */ -void *js_realloc2(JSContext *ctx, void *ptr, size_t size, size_t *pslack) +/* store extra allocated number of elements in *pslack if successful */ +static void *js_realloc_array(JSContext *ctx, void *ptr, size_t nmemb, size_t elem_size, size_t *pslack) { void *ret; + size_t size; + if (elem_size != 0 && nmemb >= ((SIZE_MAX - 4096) / elem_size)) + goto fail; + size = nmemb * elem_size; ret = js_realloc_rt(ctx->rt, ptr, size); if (unlikely(!ret && size != 0)) { + fail: JS_ThrowOutOfMemory(ctx); return NULL; } if (pslack) { size_t new_size = js_malloc_usable_size_rt(ctx->rt, ret); - *pslack = (new_size > size) ? new_size - size : 0; + *pslack = (new_size > size) ? (new_size - size) / elem_size : 0; } return ret; } +void *js_realloc2(JSContext *ctx, void *ptr, size_t size, size_t *pslack) +{ + return js_realloc_array(ctx, ptr, size, 1, pslack); +} + size_t js_malloc_usable_size(JSContext *ctx, const void *ptr) { return js_malloc_usable_size_rt(ctx->rt, ptr); @@ -1938,33 +1980,86 @@ char *js_strdup(JSContext *ctx, const char *str) return js_strndup(ctx, str, strlen(str)); } -static no_inline int js_realloc_array(JSContext *ctx, void **parray, - int elem_size, int *psize, int req_size) +static no_inline int __js_expand_array_realloc(JSContext *ctx, void **parray, + int elem_size, int *psize, int req_size, int min_size) { - int new_size; + uint32_t new_size, size; size_t slack; void *new_array; - /* XXX: potential arithmetic overflow */ - new_size = max_int(req_size, *psize * 3 / 2); - new_array = js_realloc2(ctx, *parray, new_size * elem_size, &slack); + size = *psize; + new_size = max_uint32(max_uint32(req_size, min_size), + size + (size / 2)); + if (new_size > INT32_MAX) { + JS_ThrowOutOfMemory(ctx); + return -1; + } + new_array = js_realloc_array(ctx, *parray, new_size, elem_size, &slack); if (!new_array) return -1; - new_size += slack / elem_size; + new_size += slack; *psize = new_size; *parray = new_array; return 0; } -/* resize the array and update its size if req_size > *psize */ -static inline int js_resize_array(JSContext *ctx, void **parray, int elem_size, - int *psize, int req_size) +static no_inline int __js_expand_array_static_realloc(JSContext *ctx, void **parray, + int elem_size, int *psize, int req_size, + void *static_array) +{ + uint32_t new_size, size; + size_t slack; + void *new_array, *array; + size = *psize; + new_size = max_uint32(req_size, size + (size / 2)); + if (new_size > INT32_MAX) { + JS_ThrowOutOfMemory(ctx); + return -1; + } + array = *parray; + if (array == static_array) { + new_array = js_realloc_array(ctx, NULL, new_size, elem_size, &slack); + if (!new_array) + return -1; + memcpy(new_array, static_array, size * elem_size); + } else { + new_array = js_realloc_array(ctx, array, new_size, elem_size, &slack); + if (!new_array) + return -1; + } + new_size += slack; + *psize = new_size; + *parray = new_array; + return 0; +} + +/* resize the array and update its size if req_size > *psize. The + array is allocated with at least 'min_size' elements */ +static inline int __js_expand_array(JSContext *ctx, void **parray, int elem_size, + int *psize, int req_size, int min_size) +{ + if (unlikely(req_size > *psize)) + return __js_expand_array_realloc(ctx, parray, elem_size, psize, req_size, min_size); + else + return 0; +} + +/* resize the array and update its size if req_size > *psize. The + array is allocated with at least 'min_size' elements. It is assumed + that '*parray' is initially the statically allocated buffer + 'static_buf' of size '*psize' */ +static inline int __js_expand_array_static(JSContext *ctx, void **parray, int elem_size, + int *psize, int req_size, void *static_buf) { if (unlikely(req_size > *psize)) - return js_realloc_array(ctx, parray, elem_size, psize, req_size); + return __js_expand_array_static_realloc(ctx, parray, elem_size, psize, req_size, static_buf); else return 0; } +#define js_expand_array(ctx, parray, psize, req_size) __js_expand_array(ctx, (void **)(parray), sizeof(**(parray)), psize, req_size, 1) +#define js_expand_array_min(ctx, parray, psize, req_size, min_size) __js_expand_array(ctx, (void **)(parray), sizeof(**(parray)), psize, req_size, min_size) +#define js_expand_array_static(ctx, parray, psize, req_size, static_buf) __js_expand_array_static(ctx, (void **)(parray), sizeof(**(parray)), psize, req_size, static_buf) + static inline void js_dbuf_init(JSContext *ctx, DynBuf *s) { dbuf_init2(s, ctx->rt, (DynBufReallocFunc *)js_realloc_rt); @@ -4141,7 +4236,7 @@ static no_inline int string_buffer_widen(StringBuffer *s, int size) if (s->error_status) return -1; - str = js_realloc2(s->ctx, s->str, sizeof(JSString) + (size << 1), &slack); + str = js_realloc2(s->ctx, s->str, sizeof(JSString) + ((size_t)size << 1), &slack); if (!str) return string_buffer_set_error(s); size += slack >> 1; @@ -5752,7 +5847,7 @@ static JSValue JS_NewObjectFromShape(JSContext *ctx, JSShape *sh, JSClassID clas p->weakref_count = 0; p->u.opaque = NULL; p->shape = sh; - p->prop = js_malloc(ctx, sizeof(JSProperty) * sh->prop_size); + p->prop = js_malloc_array(ctx, sh->prop_size, sizeof(JSProperty)); if (unlikely(!p->prop)) { js_free(ctx, p); fail: @@ -8873,10 +8968,8 @@ static int __exception JS_GetOwnPropertyNamesInternal(JSContext *ctx, JS_FreePropertyEnum(ctx, tab_exotic, exotic_count); return -1; } - /* XXX: need generic way to test for js_malloc(ctx, a * b) overflow */ - /* avoid allocating 0 bytes */ - tab_atom = js_malloc(ctx, sizeof(tab_atom[0]) * max_int(atom_count, 1)); + tab_atom = js_malloc_array(ctx, max_int(atom_count, 1), sizeof(tab_atom[0])); if (!tab_atom) { JS_FreePropertyEnum(ctx, tab_exotic, exotic_count); return -1; @@ -9727,18 +9820,17 @@ static int set_array_length(JSContext *ctx, JSObject *p, JSValue val, return TRUE; } -/* return -1 if exception */ +/* return -1 if exception. new_len <= INT32_MAX */ static int expand_fast_array(JSContext *ctx, JSObject *p, uint32_t new_len) { uint32_t new_size; size_t slack; JSValue *new_array_prop; - /* XXX: potential arithmetic overflow */ - new_size = max_int(new_len, p->u.array.u1.size * 3 / 2); - new_array_prop = js_realloc2(ctx, p->u.array.u.values, sizeof(JSValue) * new_size, &slack); + new_size = max_uint32(new_len, p->u.array.u1.size + p->u.array.u1.size / 2); + new_array_prop = js_realloc_array(ctx, p->u.array.u.values, new_size, sizeof(JSValue), &slack); if (!new_array_prop) return -1; - new_size += slack / sizeof(*new_array_prop); + new_size += slack; p->u.array.u.values = new_array_prop; p->u.array.u1.size = new_size; return 0; @@ -16597,7 +16689,7 @@ static JSValue js_build_arguments(JSContext *ctx, int argc, JSValueConst *argv) /* initialize the fast array part */ tab = NULL; if (argc > 0) { - tab = js_malloc(ctx, sizeof(tab[0]) * argc); + tab = js_malloc_array(ctx, argc, sizeof(tab[0])); if (!tab) goto fail; for(i = 0; i < argc; i++) { @@ -16660,7 +16752,7 @@ static JSValue js_build_mapped_arguments(JSContext *ctx, int argc, /* initialize the fast array part */ tab = NULL; if (argc > 0) { - tab = js_malloc(ctx, sizeof(tab[0]) * argc); + tab = js_malloc_array(ctx, argc, sizeof(tab[0])); if (!tab) goto fail; for(i = 0; i < arg_count; i++) { @@ -17786,7 +17878,7 @@ static JSValue js_closure2(JSContext *ctx, JSValue func_obj, p->u.func.home_object = NULL; p->u.func.var_refs = NULL; if (b->closure_var_count) { - var_refs = js_mallocz(ctx, sizeof(var_refs[0]) * b->closure_var_count); + var_refs = js_mallocz_array(ctx, b->closure_var_count, sizeof(var_refs[0])); if (!var_refs) goto fail; p->u.func.var_refs = var_refs; @@ -24552,8 +24644,7 @@ static int new_label_fd(JSFunctionDef *fd) int label; LabelSlot *ls; - if (js_resize_array(fd->ctx, (void *)&fd->label_slots, - sizeof(fd->label_slots[0]), + if (js_expand_array(fd->ctx, &fd->label_slots, &fd->label_size, fd->label_count + 1)) return -1; label = fd->label_count++; @@ -24619,7 +24710,7 @@ static int cpool_add(JSParseState *s, JSValue val) { JSFunctionDef *fd = s->cur_func; - if (js_resize_array(s->ctx, (void *)&fd->cpool, sizeof(fd->cpool[0]), + if (js_expand_array(s->ctx, &fd->cpool, &fd->cpool_size, fd->cpool_count + 1)) { JS_FreeValue(s->ctx, val); return -1; @@ -24769,25 +24860,9 @@ static int push_scope(JSParseState *s) JS_ThrowInternalError(s->ctx, "too many scopes"); return -1; } - if ((scope + 1) > fd->scope_size) { - int new_size; - size_t slack; - JSVarScope *new_buf; - /* XXX: potential arithmetic overflow */ - new_size = max_int(fd->scope_count + 1, fd->scope_size * 3 / 2); - if (fd->scopes == fd->def_scope_array) { - new_buf = js_realloc2(s->ctx, NULL, new_size * sizeof(*fd->scopes), &slack); - if (!new_buf) - return -1; - memcpy(new_buf, fd->scopes, fd->scope_count * sizeof(*fd->scopes)); - } else { - new_buf = js_realloc2(s->ctx, fd->scopes, new_size * sizeof(*fd->scopes), &slack); - if (!new_buf) - return -1; - } - new_size += slack / sizeof(*new_buf); - fd->scopes = new_buf; - fd->scope_size = new_size; + if (js_expand_array_static(s->ctx, &fd->scopes, &fd->scope_size, scope + 1, + fd->def_scope_array)) { + return -1; } fd->scope_count++; fd->scopes[scope].parent = fd->scope_level; @@ -24841,7 +24916,7 @@ static int add_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name) JS_ThrowInternalError(ctx, "too many local variables"); return -1; } - if (js_resize_array(ctx, (void **)&fd->vars, sizeof(fd->vars[0]), + if (js_expand_array(ctx, &fd->vars, &fd->var_size, fd->var_count + 1)) return -1; vd = &fd->vars[fd->var_count++]; @@ -24921,8 +24996,7 @@ static int add_arg(JSContext *ctx, JSFunctionDef *fd, JSAtom name) JS_ThrowInternalError(ctx, "too many arguments"); return -1; } - if (js_resize_array(ctx, (void **)&fd->args, sizeof(fd->args[0]), - &fd->arg_size, fd->arg_count + 1)) + if (js_expand_array(ctx, &fd->args, &fd->arg_size, fd->arg_count + 1)) return -1; vd = &fd->args[fd->arg_count++]; memset(vd, 0, sizeof(*vd)); @@ -24937,8 +25011,7 @@ static JSGlobalVar *add_global_var(JSContext *ctx, JSFunctionDef *s, { JSGlobalVar *hf; - if (js_resize_array(ctx, (void **)&s->global_vars, - sizeof(s->global_vars[0]), + if (js_expand_array(ctx, &s->global_vars, &s->global_var_size, s->global_var_count + 1)) return NULL; hf = &s->global_vars[s->global_var_count++]; @@ -30816,8 +30889,7 @@ static int add_req_module_entry(JSContext *ctx, JSModuleDef *m, { JSReqModuleEntry *rme; - if (js_resize_array(ctx, (void **)&m->req_module_entries, - sizeof(JSReqModuleEntry), + if (js_expand_array(ctx, &m->req_module_entries, &m->req_module_entries_size, m->req_module_entries_count + 1)) return -1; @@ -30859,8 +30931,7 @@ static JSExportEntry *add_export_entry2(JSContext *ctx, return NULL; } - if (js_resize_array(ctx, (void **)&m->export_entries, - sizeof(JSExportEntry), + if (js_expand_array(ctx, &m->export_entries, &m->export_entries_size, m->export_entries_count + 1)) return NULL; @@ -30885,8 +30956,7 @@ static int add_star_export_entry(JSContext *ctx, JSModuleDef *m, { JSStarExportEntry *se; - if (js_resize_array(ctx, (void **)&m->star_export_entries, - sizeof(JSStarExportEntry), + if (js_expand_array(ctx, &m->star_export_entries, &m->star_export_entries_size, m->star_export_entries_count + 1)) return -1; @@ -31223,8 +31293,7 @@ static int add_resolve_entry(JSContext *ctx, JSResolveState *s, { JSResolveEntry *re; - if (js_resize_array(ctx, (void **)&s->array, - sizeof(JSResolveEntry), + if (js_expand_array(ctx, &s->array, &s->size, s->count + 1)) return -1; re = &s->array[s->count++]; @@ -31425,7 +31494,7 @@ static __exception int get_exported_names(JSContext *ctx, if (s->modules[i] == m) return 0; } - if (js_resize_array(ctx, (void **)&s->modules, sizeof(s->modules[0]), + if (js_expand_array(ctx, &s->modules, &s->modules_size, s->modules_count + 1)) return -1; s->modules[s->modules_count++] = m; @@ -31436,7 +31505,7 @@ static __exception int get_exported_names(JSContext *ctx, continue; j = find_exported_name(s, me->export_name); if (j < 0) { - if (js_resize_array(ctx, (void **)&s->exported_names, sizeof(s->exported_names[0]), + if (js_expand_array(ctx, &s->exported_names, &s->exported_names_size, s->exported_names_count + 1)) return -1; @@ -32339,7 +32408,7 @@ static int gather_available_ancestors(JSContext *ctx, JSModuleDef *module, assert(m->pending_async_dependencies > 0); m->pending_async_dependencies--; if (m->pending_async_dependencies == 0) { - if (js_resize_array(ctx, (void **)&exec_list->tab, sizeof(exec_list->tab[0]), &exec_list->size, exec_list->count + 1)) { + if (js_expand_array(ctx, &exec_list->tab, &exec_list->size, exec_list->count + 1)) { return -1; } exec_list->tab[exec_list->count++] = m; @@ -32605,7 +32674,7 @@ static int js_inner_module_evaluation(JSContext *ctx, JSModuleDef *m, } if (m1->async_evaluation) { m->pending_async_dependencies++; - if (js_resize_array(ctx, (void **)&m1->async_parent_modules, sizeof(m1->async_parent_modules[0]), &m1->async_parent_modules_size, m1->async_parent_modules_count + 1)) { + if (js_expand_array(ctx, &m1->async_parent_modules, &m1->async_parent_modules_size, m1->async_parent_modules_count + 1)) { *pvalue = JS_GetException(ctx); return -1; } @@ -33040,8 +33109,7 @@ static int add_import(JSParseState *s, JSModuleDef *m, local_name, TRUE, TRUE, JS_VAR_NORMAL); if (var_idx < 0) return -1; - if (js_resize_array(ctx, (void **)&m->import_entries, - sizeof(JSImportEntry), + if (js_expand_array(ctx, &m->import_entries, &m->import_entries_size, m->import_entries_count + 1)) return -1; @@ -33419,7 +33487,7 @@ static void dump_byte_code(JSContext *ctx, int pass, { const JSOpCode *oi; int pos, pos_next, op, size, idx, addr, line, line1, in_source, line_num; - uint8_t *bits = js_mallocz(ctx, len * sizeof(*bits)); + uint8_t *bits = js_mallocz_array(ctx, len, sizeof(*bits)); BOOL use_short_opcodes = (b != NULL), dump_pc; if (b) { @@ -33867,8 +33935,7 @@ static int add_closure_var(JSContext *ctx, JSFunctionDef *s, return -1; } - if (js_resize_array(ctx, (void **)&s->closure_var, - sizeof(s->closure_var[0]), + if (js_expand_array(ctx, &s->closure_var, &s->closure_var_size, s->closure_var_count + 1)) return -1; cv = &s->closure_var[s->closure_var_count++]; @@ -34979,7 +35046,7 @@ static __exception int add_closure_variables(JSContext *ctx, JSFunctionDef *s, s->closure_var_size = count; if (count == 0) return 0; - s->closure_var = js_malloc(ctx, sizeof(s->closure_var[0]) * count); + s->closure_var = js_malloc_array(ctx, count, sizeof(s->closure_var[0])); if (!s->closure_var) return -1; /* Add lexical variables in scope at the point of evaluation */ @@ -36011,14 +36078,14 @@ static __exception int resolve_labels(JSContext *ctx, JSFunctionDef *s) #if SHORT_OPCODES if (s->jump_size) { - s->jump_slots = js_mallocz(s->ctx, sizeof(*s->jump_slots) * s->jump_size); + s->jump_slots = js_mallocz_array(s->ctx, s->jump_size, sizeof(*s->jump_slots)); if (s->jump_slots == NULL) goto fail; } #endif /* XXX: Should skip this phase if not generating SHORT_OPCODES */ if (s->line_number_size && !s->strip_debug) { - s->line_number_slots = js_mallocz(s->ctx, sizeof(*s->line_number_slots) * s->line_number_size); + s->line_number_slots = js_mallocz_array(s->ctx, s->line_number_size, sizeof(*s->line_number_slots)); if (s->line_number_slots == NULL) goto fail; s->line_number_last = s->source_pos; @@ -36940,7 +37007,7 @@ static __exception int ss_check(JSContext *ctx, StackSizeState *s, s->catch_pos_tab[pos] = catch_pos; /* queue the new PC to explore */ - if (js_resize_array(ctx, (void **)&s->pc_stack, sizeof(s->pc_stack[0]), + if (js_expand_array(ctx, &s->pc_stack, &s->pc_stack_size, s->pc_stack_len + 1)) return -1; s->pc_stack[s->pc_stack_len++] = pos; @@ -36959,15 +37026,13 @@ static __exception int compute_stack_size(JSContext *ctx, bc_buf = fd->byte_code.buf; s->bc_len = fd->byte_code.size; /* bc_len > 0 */ - s->stack_level_tab = js_malloc(ctx, sizeof(s->stack_level_tab[0]) * - s->bc_len); + s->stack_level_tab = js_malloc_array(ctx, s->bc_len, sizeof(s->stack_level_tab[0])); if (!s->stack_level_tab) return -1; for(i = 0; i < s->bc_len; i++) s->stack_level_tab[i] = 0xffff; s->pc_stack = NULL; - s->catch_pos_tab = js_malloc(ctx, sizeof(s->catch_pos_tab[0]) * - s->bc_len); + s->catch_pos_tab = js_malloc_array(ctx, s->bc_len, sizeof(s->catch_pos_tab[0])); if (!s->catch_pos_tab) goto fail; @@ -38654,7 +38719,7 @@ static int js_object_list_resize_hash(JSContext *ctx, JSObjectList *s, JSObjectListEntry *e; uint32_t i, h, *new_hash_table; - new_hash_table = js_malloc(ctx, sizeof(new_hash_table[0]) * new_hash_size); + new_hash_table = js_malloc_array(ctx, new_hash_size, sizeof(new_hash_table[0])); if (!new_hash_table) return -1; js_free(ctx, s->hash_table); @@ -38680,8 +38745,7 @@ static int js_object_list_add(JSContext *ctx, JSObjectList *s, JSObject *obj) JSObjectListEntry *e; uint32_t h, new_hash_size; - if (js_resize_array(ctx, (void *)&s->object_tab, - sizeof(s->object_tab[0]), + if (js_expand_array(ctx, &s->object_tab, &s->object_size, s->object_count + 1)) return -1; if (unlikely((s->object_count + 1) >= s->hash_size)) { @@ -38863,16 +38927,14 @@ static int bc_atom_to_idx(BCWriterState *s, uint32_t *pres, JSAtom atom) if (atom >= s->atom_to_idx_size) { int old_size, i; old_size = s->atom_to_idx_size; - if (js_resize_array(s->ctx, (void **)&s->atom_to_idx, - sizeof(s->atom_to_idx[0]), &s->atom_to_idx_size, - atom + 1)) + if (js_expand_array(s->ctx, &s->atom_to_idx, + &s->atom_to_idx_size, atom + 1)) return -1; - /* XXX: could add a specific js_resize_array() function to do it */ + /* XXX: could add a specific js_expand_array() function to do it */ for(i = old_size; i < s->atom_to_idx_size; i++) s->atom_to_idx[i] = 0; } - if (js_resize_array(s->ctx, (void **)&s->idx_to_atom, - sizeof(s->idx_to_atom[0]), + if (js_expand_array(s->ctx, &s->idx_to_atom, &s->idx_to_atom_size, s->idx_to_atom_count + 1)) goto fail; @@ -39369,7 +39431,7 @@ static int JS_WriteSharedArrayBuffer(BCWriterState *s, JSValueConst obj) bc_put_leb128(s, abuf->byte_length); bc_put_leb128(s, abuf->max_byte_length); bc_put_u64(s, (uintptr_t)abuf->data); - if (js_resize_array(s->ctx, (void **)&s->sab_tab, sizeof(s->sab_tab[0]), + if (js_expand_array(s->ctx, &s->sab_tab, &s->sab_tab_size, s->sab_tab_len + 1)) return -1; /* keep the SAB pointer so that the user can clone it or free it */ @@ -39974,8 +40036,7 @@ static JSValue JS_ReadObjectRec(BCReaderState *s); static int BC_add_object_ref1(BCReaderState *s, JSObject *p) { if (s->allow_reference) { - if (js_resize_array(s->ctx, (void *)&s->objects, - sizeof(s->objects[0]), + if (js_expand_array(s->ctx, &s->objects, &s->objects_size, s->objects_count + 1)) return -1; s->objects[s->objects_count++] = p; @@ -40212,7 +40273,7 @@ static JSValue JS_ReadModule(BCReaderState *s) goto fail; if (m->req_module_entries_count != 0) { m->req_module_entries_size = m->req_module_entries_count; - m->req_module_entries = js_mallocz(ctx, sizeof(m->req_module_entries[0]) * m->req_module_entries_size); + m->req_module_entries = js_mallocz_array(ctx, m->req_module_entries_size, sizeof(m->req_module_entries[0])); if (!m->req_module_entries) goto fail; for(i = 0; i < m->req_module_entries_count; i++) { @@ -40231,7 +40292,7 @@ static JSValue JS_ReadModule(BCReaderState *s) goto fail; if (m->export_entries_count != 0) { m->export_entries_size = m->export_entries_count; - m->export_entries = js_mallocz(ctx, sizeof(m->export_entries[0]) * m->export_entries_size); + m->export_entries = js_mallocz_array(ctx, m->export_entries_size, sizeof(m->export_entries[0])); if (!m->export_entries) goto fail; for(i = 0; i < m->export_entries_count; i++) { @@ -40257,7 +40318,7 @@ static JSValue JS_ReadModule(BCReaderState *s) goto fail; if (m->star_export_entries_count != 0) { m->star_export_entries_size = m->star_export_entries_count; - m->star_export_entries = js_mallocz(ctx, sizeof(m->star_export_entries[0]) * m->star_export_entries_size); + m->star_export_entries = js_mallocz_array(ctx, m->star_export_entries_size, sizeof(m->star_export_entries[0])); if (!m->star_export_entries) goto fail; for(i = 0; i < m->star_export_entries_count; i++) { @@ -40271,7 +40332,7 @@ static JSValue JS_ReadModule(BCReaderState *s) goto fail; if (m->import_entries_count != 0) { m->import_entries_size = m->import_entries_count; - m->import_entries = js_mallocz(ctx, sizeof(m->import_entries[0]) * m->import_entries_size); + m->import_entries = js_mallocz_array(ctx, m->import_entries_size, sizeof(m->import_entries[0])); if (!m->import_entries) goto fail; for(i = 0; i < m->import_entries_count; i++) { @@ -40690,8 +40751,8 @@ static int JS_ReadObjectAtoms(BCReaderState *s) bc_read_trace(s, "%d atom indexes {\n", s->idx_to_atom_count); if (s->idx_to_atom_count != 0) { - s->idx_to_atom = js_mallocz(s->ctx, s->idx_to_atom_count * - sizeof(s->idx_to_atom[0])); + s->idx_to_atom = js_mallocz_array(s->ctx, s->idx_to_atom_count, + sizeof(s->idx_to_atom[0])); if (!s->idx_to_atom) return s->error_state = -1; } @@ -41319,7 +41380,7 @@ static __exception int JS_ObjectDefineProperties(JSContext *ctx, p = JS_VALUE_GET_OBJ(props); if (JS_GetOwnPropertyNamesInternal(ctx, &atoms, &len, p, JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK) < 0) goto exception; - descs = js_malloc(ctx, sizeof(descs[0]) * len); + descs = js_malloc_array(ctx, len, sizeof(descs[0])); if (!descs) goto exception; for(i = 0; i < len; i++) @@ -42440,7 +42501,7 @@ static JSValue *build_arg_list(JSContext *ctx, uint32_t *plen, } len = len64; /* avoid allocating 0 bytes */ - tab = js_mallocz(ctx, sizeof(tab[0]) * max_uint32(1, len)); + tab = js_mallocz_array(ctx, max_uint32(1, len), sizeof(tab[0])); if (!tab) return NULL; p = JS_VALUE_GET_OBJ(array_arg); @@ -45294,10 +45355,10 @@ static JSValue js_array_sort(JSContext *ctx, JSValueConst this_val, size_t new_size, slack; ValueSlot *new_array; new_size = (array_size + (array_size >> 1) + 31) & ~15; - new_array = js_realloc2(ctx, array, new_size * sizeof(*array), &slack); + new_array = js_realloc_array(ctx, array, new_size, sizeof(*array), &slack); if (new_array == NULL) goto exception; - new_size += slack / sizeof(*new_array); + new_size += slack; array = new_array; array_size = new_size; } @@ -46024,7 +46085,7 @@ static JSValue js_iterator_zip_next(JSContext *ctx, JSValueConst this_val, } while (TRUE) { - results = js_malloc(ctx, it->count * sizeof(*results)); + results = js_malloc_array(ctx, it->count, sizeof(*results)); if (!results) { ret = JS_EXCEPTION; goto fail; @@ -46269,11 +46330,11 @@ static JSValue js_iterator_zip(JSContext *ctx, JSValueConst this_val, item = JS_UNDEFINED; if (count == cap) { newcap = cap ? cap + (cap / 2) : 4; - tab = js_realloc(ctx, iters, sizeof(*tab) * newcap); + tab = js_realloc_array(ctx, iters, newcap, sizeof(*tab), NULL); if (!tab) goto fail_iters; iters = tab; - tab = js_realloc(ctx, nexts, sizeof(*tab) * newcap); + tab = js_realloc_array(ctx, nexts, newcap, sizeof(*tab), NULL); if (!tab) goto fail_iters; nexts = tab; @@ -46331,15 +46392,15 @@ static JSValue js_iterator_zip(JSContext *ctx, JSValueConst this_val, value = JS_UNDEFINED; if (count == cap) { newcap = cap ? cap * 2 : 4; - tab = js_realloc(ctx, iters, sizeof(*tab) * newcap); + tab = js_realloc_array(ctx, iters, newcap, sizeof(*tab), NULL); if (!tab) goto fail_iters; iters = tab; - tab = js_realloc(ctx, nexts, sizeof(*tab) * newcap); + tab = js_realloc_array(ctx, nexts, newcap, sizeof(*tab), NULL); if (!tab) goto fail_iters; nexts = tab; - atab = js_realloc(ctx, keys, sizeof(*atab) * newcap); + atab = js_realloc_array(ctx, keys, newcap, sizeof(*atab), NULL); if (!atab) goto fail_iters; keys = atab; @@ -46362,7 +46423,7 @@ static JSValue js_iterator_zip(JSContext *ctx, JSValueConst this_val, } /* padding values */ - padding = js_malloc(ctx, (count + 1) * sizeof(*padding)); + padding = js_malloc_array(ctx, count + 1, sizeof(*padding)); if (!padding) goto fail_iters; for (i = 0; i <= count; i++) @@ -47220,7 +47281,7 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, memmove(it->buf, &it->buf[1], it->buf_count * sizeof(*it->buf)); } - if (js_resize_array(ctx, (void **)&it->buf, sizeof(*it->buf), + if (js_expand_array(ctx, &it->buf, &it->buf_size, it->buf_count + 1)) { JS_FreeValue(ctx, item); goto fail_no_close; @@ -49411,7 +49472,7 @@ int JS_ToUTF32String(JSContext *ctx, uint32_t **pbuf, JSValueConst val1) p = JS_VALUE_GET_STRING(val); len = p->len; /* UTF32 buffer length is len minus the number of correct surrogates pairs */ - buf = js_malloc(ctx, sizeof(buf[0]) * max_int(len, 1)); + buf = js_malloc_array(ctx, max_int(len, 1), sizeof(buf[0])); if (!buf) { JS_FreeValue(ctx, val); goto fail; @@ -51017,7 +51078,7 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, str = JS_VALUE_GET_STRING(str_val); alloc_count = lre_get_alloc_count(re_bytecode); if (alloc_count > 0) { - capture = js_malloc(ctx, sizeof(capture[0]) * alloc_count); + capture = js_malloc_array(ctx, alloc_count, sizeof(capture[0])); if (!capture) goto fail; } @@ -51240,7 +51301,7 @@ static JSValue js_regexp_replace(JSContext *ctx, JSValueConst this_val, JSValueC } alloc_count = lre_get_alloc_count(re_bytecode); if (alloc_count > 0) { - capture = js_malloc(ctx, sizeof(capture[0]) * alloc_count); + capture = js_malloc_array(ctx, alloc_count, sizeof(capture[0])); if (!capture) goto fail; } @@ -51593,9 +51654,9 @@ static int value_buffer_init(JSContext *ctx, ValueBuffer *b) { b->ctx = ctx; b->len = 0; - b->size = 4; b->error_status = 0; b->arr = b->def; + b->size = countof(b->def); return 0; } @@ -51613,28 +51674,11 @@ static int value_buffer_append(ValueBuffer *b, JSValue val) { if (b->error_status) return -1; - - if (b->len >= b->size) { - int new_size = (b->len + (b->len >> 1) + 31) & ~16; - size_t slack; - JSValue *new_arr; - - if (b->arr == b->def) { - new_arr = js_realloc2(b->ctx, NULL, sizeof(*b->arr) * new_size, &slack); - if (new_arr) - memcpy(new_arr, b->def, sizeof b->def); - } else { - new_arr = js_realloc2(b->ctx, b->arr, sizeof(*b->arr) * new_size, &slack); - } - if (!new_arr) { - value_buffer_free(b); - JS_FreeValue(b->ctx, val); - b->error_status = -1; - return -1; - } - new_size += slack / sizeof(*new_arr); - b->arr = new_arr; - b->size = new_size; + if (js_expand_array_static(b->ctx, &b->arr, &b->size, b->len + 1, b->def)) { + value_buffer_free(b); + JS_FreeValue(b->ctx, val); + b->error_status = -1; + return -1; } b->arr[b->len++] = val; return 0; @@ -52252,7 +52296,7 @@ static int json_parse_record_resize_hash(JSContext *ctx, JSONParseRecordObject * uint32_t i, h, *new_hash_table; JSONParseRecordEntry *e; - new_hash_table = js_malloc(ctx, sizeof(new_hash_table[0]) * new_hash_size); + new_hash_table = js_malloc_array(ctx, new_hash_size, sizeof(new_hash_table[0])); if (!new_hash_table) return -1; js_free(ctx, po->hash_table); @@ -52278,7 +52322,7 @@ static JSONParseRecord *json_parse_record_add(JSContext *ctx, JSONParseRecord *p JSONParseRecord *pr1; uint32_t h; - if (js_resize_array(ctx, (void **)&po->entries, sizeof(po->entries[0]), + if (js_expand_array(ctx, &po->entries, psize, po->count + 1)) { return NULL; } @@ -52444,7 +52488,7 @@ static JSValue json_parse_value(JSParseState *s, JSONParseRecord *pr) idx = 0; for(;;) { if (pr) { - if (js_resize_array(ctx, (void **)&pr->u.array.elements, sizeof(pr->u.array.elements[0]), + if (js_expand_array(ctx, &pr->u.array.elements, &pr_size, pr->u.array.count + 1)) goto fail; pr1 = &pr->u.array.elements[pr->u.array.count++]; @@ -54158,7 +54202,7 @@ static int js_proxy_get_own_property_names(JSContext *ctx, } len = len64; if (len > 0) { - tab = js_mallocz(ctx, sizeof(tab[0]) * len); + tab = js_mallocz_array(ctx, len, sizeof(tab[0])); if (!tab) goto fail; } @@ -54708,7 +54752,7 @@ static JSValue js_map_constructor(JSContext *ctx, JSValueConst new_target, JS_SetOpaque(obj, s); s->hash_bits = 1; s->hash_size = 1U << s->hash_bits; - s->hash_table = js_mallocz(ctx, sizeof(s->hash_table[0]) * s->hash_size); + s->hash_table = js_mallocz_array(ctx, s->hash_size, sizeof(s->hash_table[0])); if (!s->hash_table) goto fail; s->record_count_threshold = 4; @@ -54920,8 +54964,8 @@ static void map_hash_resize(JSContext *ctx, JSMapState *s) /* XXX: no reporting of memory allocation failure */ new_hash_bits = min_int(s->hash_bits + 1, 31); new_hash_size = 1U << new_hash_bits; - new_hash_table = js_realloc(ctx, s->hash_table, - sizeof(new_hash_table[0]) * new_hash_size); + new_hash_table = js_realloc_array(ctx, s->hash_table, + new_hash_size, sizeof(new_hash_table[0]), NULL); if (!new_hash_table) return; @@ -56368,15 +56412,9 @@ static int js_disposable_stack_add(JSContext *ctx, JSDisposableStack *ds, JSValueConst value, JSValueConst method, int hint) { - if (ds->resource_count >= ds->resource_capacity) { - int new_cap = max_int(ds->resource_capacity * 2, 4); - JSDisposableResource *new_res; - new_res = js_realloc(ctx, ds->resources, - new_cap * sizeof(JSDisposableResource)); - if (!new_res) + if (js_expand_array_min(ctx, &ds->resources, &ds->resource_capacity, + ds->resource_count + 1, 4)) { return -1; - ds->resources = new_res; - ds->resource_capacity = new_cap; } ds->resources[ds->resource_count].value = JS_DupValue(ctx, value); ds->resources[ds->resource_count].method = JS_DupValue(ctx, method); @@ -62973,13 +63011,13 @@ static JSValue js_typed_array_sort(JSContext *ctx, JSValueConst this_val, /* the array must be copied because the comparison function may modify it */ - array = js_malloc(ctx, len * elt_size); + array = js_malloc_array(ctx, len, elt_size); if (!array) return JS_EXCEPTION; memcpy(array, p->u.array.u.ptr, len * elt_size); /* array_idx is needed to have a stable sort */ - array_idx = js_malloc(ctx, len * sizeof(array_idx[0])); + array_idx = js_malloc_array(ctx, len, sizeof(array_idx[0])); if (!array_idx) { js_free(ctx, array); return JS_EXCEPTION; -- 2.47.3