Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/jsc/VirtualMachine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4558,6 +4558,7 @@ impl VirtualMachine {
allow_side_effects: bool,
) {
let mut formatter = crate::console_object::Formatter::new(self.global());
formatter.stack_check = bun_core::StackCheck::init();
let colors = bun_core::Output::enable_ansi_colors_stderr();
self.print_errorlike_object(
exception.value(),
Expand Down Expand Up @@ -4869,6 +4870,17 @@ impl VirtualMachine {
// once the AggregateError branch is taken).
let global_ref = self.global();

// Stack-safety guard for the AggregateError recursion below (`agg_iter`
// → `print_errorlike_object`). The `cause` chain is already guarded in
// `print_error_instance_js`; this covers the `.errors` chain.
if !formatter.stack_check.is_safe_to_recurse() {
formatter.failed = true;
if formatter.can_throw_stack_overflow {
let _ = global_ref.throw_stack_overflow();
}
return;
}

if value.is_aggregate_error(global_ref) {
// Note: `JSValue::for_each` takes a C-ABI fn
// pointer + erased ctx, so thread the captures through a struct.
Expand Down
1 change: 1 addition & 0 deletions src/runtime/jsc_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ fn print_exception(
vm_ref.print_exception(exception, exception_list, writer, true);
} else {
let mut formatter = bun_jsc::console_object::Formatter::new(global);
formatter.stack_check = bun_core::StackCheck::init();
// `Formatter::new` already
// defaults `error_display_level` to `Full` (ConsoleObject.rs:1176).
let colors = bun_core::Output::enable_ansi_colors_stderr();
Expand Down
33 changes: 33 additions & 0 deletions test/js/node/util/bun-inspect.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from "bun:test";
import stripAnsi from "strip-ansi";
import { bunEnv, bunExe } from "harness";

describe("Bun.inspect", () => {
it("reports error instead of [native code]", () => {
Expand Down Expand Up @@ -87,6 +88,38 @@ describe("Bun.inspect", () => {
);
});

it("stack overflow is thrown when it should be for AggregateError", () => {
let e: unknown = new Error("leaf");
for (let i = 0; i < 16 * 1024; i++) {
e = new AggregateError([e], "agg");
}

expect(() => Bun.inspect(e)).toThrowErrorMatchingInlineSnapshot(`"Maximum call stack size exceeded."`);
});

describe.each(["log", "throw", "reject", "cause"])("printing a deeply nested error via %s", face => {
it.concurrent("does not crash", async () => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
const src =
face === "cause"
? `let e = new Error("leaf"); for (let i = 0; i < 16 * 1024; i++) e = new Error("c", { cause: e }); throw e;`
: `let e = new Error("leaf"); for (let i = 0; i < 16 * 1024; i++) e = new AggregateError([e], "agg");` +
{
log: ` console.log(e);`,
throw: ` throw e;`,
reject: ` Promise.reject(e); await 0;`,
}[face];
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", src],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [, , exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(proc.signalCode).toBeNull();
expect(exitCode).toBe(1);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
});
});

it("depth = 0", () => {
expect(Bun.inspect({ a: { b: { c: { d: 1 } } } }, { depth: 0 })).toEqual("{\n a: [Object ...],\n}");
});
Expand Down
Loading