Skip to content

Commit 55db71e

Browse files
authored
Fix calling build_backtrace too often (#906)
Bug introduced in commit 4c32c53 from late last month. When unwinding the stack, call build_backtrace only when the exception object doesn't already have a .stack property, like how it was before commit 4c32c53. Fixes: #904
1 parent f507557 commit 55db71e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

quickjs.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14817,6 +14817,18 @@ static void dump_single_byte_code(JSContext *ctx, const uint8_t *pc,
1481714817
static void print_func_name(JSFunctionBytecode *b);
1481814818
#endif
1481914819

14820+
static bool needs_backtrace(JSValue exc)
14821+
{
14822+
JSObject *p;
14823+
14824+
if (JS_VALUE_GET_TAG(exc) != JS_TAG_OBJECT)
14825+
return false;
14826+
p = JS_VALUE_GET_OBJ(exc);
14827+
if (p->class_id != JS_CLASS_ERROR)
14828+
return false;
14829+
return !find_own_property1(p, JS_ATOM_stack);
14830+
}
14831+
1482014832
/* argv[] is modified if (flags & JS_CALL_FLAG_COPY_ARGV) = 0. */
1482114833
static JSValue JS_CallInternal(JSContext *caller_ctx, JSValue func_obj,
1482214834
JSValue this_obj, JSValue new_target,
@@ -17290,8 +17302,12 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValue func_obj,
1729017302
}
1729117303
}
1729217304
exception:
17293-
sf->cur_pc = pc;
17294-
build_backtrace(ctx, rt->current_exception, JS_UNDEFINED, NULL, 0, 0, 0);
17305+
if (needs_backtrace(rt->current_exception)
17306+
|| JS_IsUndefined(ctx->error_back_trace)) {
17307+
sf->cur_pc = pc;
17308+
build_backtrace(ctx, rt->current_exception, JS_UNDEFINED,
17309+
NULL, 0, 0, 0);
17310+
}
1729517311
if (!JS_IsUncatchableError(ctx, rt->current_exception)) {
1729617312
while (sp > stack_buf) {
1729717313
JSValue val = *--sp;
@@ -33325,9 +33341,15 @@ static JSValue JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3332533341
const char *input, size_t input_len,
3332633342
const char *filename, int line, int flags, int scope_idx)
3332733343
{
33344+
JSRuntime *rt = ctx->rt;
33345+
3332833346
if (unlikely(!ctx->eval_internal)) {
3332933347
return JS_ThrowTypeError(ctx, "eval is not supported");
3333033348
}
33349+
if (!rt->current_stack_frame) {
33350+
JS_FreeValueRT(rt, ctx->error_back_trace);
33351+
ctx->error_back_trace = JS_UNDEFINED;
33352+
}
3333133353
return ctx->eval_internal(ctx, this_obj, input, input_len, filename, line,
3333233354
flags, scope_idx);
3333333355
}

tests/bug904.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {assert, assertThrows} from "./assert.js"
2+
let calls = 0
3+
Error.prepareStackTrace = function() { calls++ }
4+
function f() { f() }
5+
assertThrows(RangeError, f)
6+
assert(calls, 0)

0 commit comments

Comments
 (0)