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
6 changes: 6 additions & 0 deletions src/bun.js/VirtualMachine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,12 @@ pub fn resolveMaybeNeedsTrailingSlash(
jsc_vm.log = old_log;
jsc_vm.transpiler.linker.log = old_log;
jsc_vm.transpiler.resolver.log = old_log;
// The package manager may have been initialized during this resolve
// with a pointer to the stack-local `log`. Restore it so it doesn't
// dangle after this frame returns.
if (jsc_vm.transpiler.resolver.package_manager) |pm| {
pm.log = old_log;
}
}
jsc_vm._resolve(&result, specifier_utf8.slice(), normalizeSource(source_utf8.slice()), is_esm, is_a_file_path) catch |err_| {
var err = err_;
Expand Down
31 changes: 31 additions & 0 deletions test/js/bun/test/mock/mock-module-resolve-crash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";

// Regression test: mock.module with a non-string specifier (e.g. Intl.Segmenter)
// used to cause a stack-buffer-overflow because the PackageManager singleton could
// capture a dangling pointer to a stack-local Log during module resolution.
test("mock.module with non-string specifier does not crash", async () => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`
const { mock } = require("bun:test");
try {
mock.module(Intl.Segmenter, () => ({ default: 1 }));
} catch (e) {
// Expected to throw - we just need it not to crash
}
console.log("ok");
`,
],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stdout).toContain("ok");
expect(exitCode).toBe(0);
});
Loading