Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 29 additions & 0 deletions test/js/node/util/bun-inspect.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from "bun:test";
import { bunEnv, bunExe } from "harness";
import stripAnsi from "strip-ansi";

describe("Bun.inspect", () => {
Expand Down Expand Up @@ -87,6 +88,34 @@ 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."`);
});

it.concurrent.each([
["log", `e = new AggregateError([e], "agg");`, `console.log(e);`],
["throw", `e = new AggregateError([e], "agg");`, `throw e;`],
["reject", `e = new AggregateError([e], "agg");`, `Promise.reject(e); await 0;`],
["cause", `e = new Error("c", { cause: e });`, `throw e;`],
])("printing a deeply nested error via %s does not crash", async (_, wrap, emit) => {
const src = `let e = new Error("leaf"); for (let i = 0; i < 16 * 1024; i++) ${wrap} process.stderr.write("built\\n"); ${emit}`;
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", src],
env: bunEnv,
stdout: "ignore",
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect(stderr.slice(0, 6)).toBe("built\n");
expect(proc.signalCode).toBeNull();
expect(exitCode).toBe(1);
});

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