mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
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:
@@ -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());
|
||||
|
||||
|
||||
44
test/regression/issue/prepare-stack-trace-crash.test.ts
Normal file
44
test/regression/issue/prepare-stack-trace-crash.test.ts
Normal 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");
|
||||
});
|
||||
Reference in New Issue
Block a user