Fix issue with Error.prepareStackTrace (#21829)

Fixes #21815

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
pfg
2025-08-19 18:08:00 -07:00
committed by GitHub
parent decf84c416
commit 9ad5d3c6c3
2 changed files with 47 additions and 0 deletions

View File

@@ -2644,6 +2644,9 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionDefaultErrorPrepareStackTrace, (JSGlobalObjec
throwTypeError(lexicalGlobalObject, scope, "First argument must be an Error object"_s);
return {};
}
if (!callSites) {
callSites = JSArray::create(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(JSC::ArrayWithContiguous), 0);
}
JSValue result = formatStackTraceToJSValue(vm, globalObject, lexicalGlobalObject, errorObject, callSites, jsUndefined());

View File

@@ -0,0 +1,44 @@
import { expect, test } from "bun:test";
test("Error.prepareStackTrace should not crash when stacktrace parameter is not an array", () => {
const e = new Error("test message");
try {
// Test with undefined as second argument (Node errors with 'TypeError: Cannot read properties of undefined' in this case)
const result = Error.prepareStackTrace(e);
} catch (e) {}
try {
// Test with null as second argument (Node errors with 'TypeError: Cannot read properties of null' in this case)
const result = Error.prepareStackTrace(e, null);
} catch (e) {}
{
// Test with number as second argument (Node does the equivalent of Error.prepareStackTrace(e, [""]) in this case)
const result = Error.prepareStackTrace(e, 123);
expect(typeof result).toBe("string");
}
{
// Test with string as second argument (Node does the equivalent of Error.prepareStackTrace(e, [""]) in this case)
const result = Error.prepareStackTrace(e, "not an array");
expect(typeof result).toBe("string");
}
{
// Test with object as second argument (Node does the equivalent of Error.prepareStackTrace(e, [""]) in this case)
const result = Error.prepareStackTrace(e, {});
expect(typeof result).toBe("string");
}
});
test("Error.prepareStackTrace should work with empty message", () => {
const e = new Error("");
const result = Error.prepareStackTrace(e);
expect(typeof result).toBe("string");
expect(result).toBe("Error");
});
test("Error.prepareStackTrace should work with no message", () => {
const e = new Error();
const result = Error.prepareStackTrace(e);
expect(typeof result).toBe("string");
expect(result).toBe("Error");
});