Compare commits

...

3 Commits

Author SHA1 Message Date
autofix-ci[bot]
652e411338 [autofix.ci] apply automated fixes 2025-08-14 04:29:50 +00:00
Claude Bot
d7bf3a6ceb Add Node.js behavior comparison and additional test case
Enhanced the regression test to document the difference between Node.js
and Bun error messages, and added a test case to ensure custom
prepareStackTrace functions continue to work as expected.
2025-08-14 04:26:16 +00:00
Claude Bot
43e2b32989 Fix Error.prepareStackTrace crash when called with insufficient arguments
Added parameter validation to prevent segmentation fault when
Error.prepareStackTrace() is called without the required second
parameter (callSites array).

Previously, calling Error.prepareStackTrace(error) without the second
parameter would cause a segmentation fault at address 0x4. Now it
properly throws a TypeError with the message "Second argument must
be an array of call sites".

This matches the expected Node.js/V8 behavior where prepareStackTrace
requires both error and callSites parameters.

Fixes #21815

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-14 04:23:34 +00:00
2 changed files with 65 additions and 0 deletions

View File

@@ -2645,6 +2645,11 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionDefaultErrorPrepareStackTrace, (JSGlobalObjec
return {};
}
if (!callSites) {
throwTypeError(lexicalGlobalObject, scope, "Second argument must be an array of call sites"_s);
return {};
}
JSValue result = formatStackTraceToJSValue(vm, globalObject, lexicalGlobalObject, errorObject, callSites, jsUndefined());
RETURN_IF_EXCEPTION(scope, {});

View File

@@ -0,0 +1,60 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
test("Error.prepareStackTrace called without second parameter should not crash", async () => {
// This test is for issue #21815
// The command `Error.prepareStackTrace(e)` was causing a segmentation fault
// due to missing validation of the second parameter (callSites array)
//
// Node.js throws: "Cannot read properties of undefined (reading 'length')"
// Bun now throws: "Second argument must be an array of call sites" (clearer message)
const code = `
const e = new Error();
try {
Error.prepareStackTrace(e);
} catch (err) {
console.log("Caught error:", err.message);
process.exit(0);
}
console.log("No error thrown - this is unexpected");
process.exit(1);
`;
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", code],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
// The command should execute successfully without crashing
expect(exitCode).toBe(0);
expect(stdout).toContain("Caught error:");
expect(stdout).toContain("Second argument must be an array of call sites");
expect(stderr).toBe("");
});
test("Error.prepareStackTrace with custom function should work like Node.js", async () => {
// Custom prepareStackTrace functions should work normally, even with missing parameters
const code = `
Error.prepareStackTrace = (err, stack) => "Custom: " + err.message;
const e = new Error("test");
console.log("Result:", Error.prepareStackTrace(e)); // Missing second param should be OK
`;
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", code],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(exitCode).toBe(0);
expect(stdout).toContain("Result: Custom: test");
expect(stderr).toBe("");
});