Skip to content

Commit 76b3b12

Browse files
authored
Fix leak in JS_NewArrayFrom at OOM (#1176)
If it "takes ownership of |values|", it must also free them on exception.
1 parent 20b25df commit 76b3b12

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

quickjs.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5232,21 +5232,26 @@ JSValue JS_NewArrayFrom(JSContext *ctx, int count, const JSValue *values)
52325232
{
52335233
JSObject *p;
52345234
JSValue obj;
5235+
int i;
52355236

52365237
obj = JS_NewArray(ctx);
52375238
if (JS_IsException(obj))
5238-
return JS_EXCEPTION;
5239+
goto exception;
52395240
if (count > 0) {
52405241
p = JS_VALUE_GET_OBJ(obj);
52415242
if (expand_fast_array(ctx, p, count)) {
52425243
JS_FreeValue(ctx, obj);
5243-
return JS_EXCEPTION;
5244+
goto exception;
52445245
}
52455246
p->u.array.count = count;
52465247
p->prop[0].u.value = js_int32(count);
52475248
memcpy(p->u.array.u.values, values, count * sizeof(*values));
52485249
}
52495250
return obj;
5251+
exception:
5252+
for (i = 0; i < count; i++)
5253+
JS_FreeValue(ctx, values[i]);
5254+
return JS_EXCEPTION;
52505255
}
52515256

52525257
JSValue JS_NewObject(JSContext *ctx)
@@ -16636,9 +16641,9 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1663616641
pc += 2;
1663716642
call_argv = sp - call_argc;
1663816643
ret_val = JS_NewArrayFrom(ctx, call_argc, call_argv);
16644+
sp -= call_argc;
1663916645
if (unlikely(JS_IsException(ret_val)))
1664016646
goto exception;
16641-
sp -= call_argc;
1664216647
*sp++ = ret_val;
1664316648
}
1664416649
BREAK;

0 commit comments

Comments
 (0)