Skip to content

Commit 2e0f183

Browse files
committed
util: inspect: do not crash on an Error stack that contains a Symbol
See #56570
1 parent bf59539 commit 2e0f183

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

lib/internal/util/inspect.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -1287,8 +1287,14 @@ function identicalSequenceRange(a, b) {
12871287
return { len: 0, offset: 0 };
12881288
}
12891289

1290-
function getStackString(error) {
1291-
return error.stack ? String(error.stack) : ErrorPrototypeToString(error);
1290+
function getStackString(ctx, error) {
1291+
if (error.stack) {
1292+
if (typeof error.stack === 'string') {
1293+
return error.stack;
1294+
}
1295+
return formatValue(ctx, error.stack);
1296+
}
1297+
return ErrorPrototypeToString(error);
12921298
}
12931299

12941300
function getStackFrames(ctx, err, stack) {
@@ -1303,7 +1309,7 @@ function getStackFrames(ctx, err, stack) {
13031309

13041310
// Remove stack frames identical to frames in cause.
13051311
if (cause != null && isError(cause)) {
1306-
const causeStack = getStackString(cause);
1312+
const causeStack = getStackString(ctx, cause);
13071313
const causeStackStart = StringPrototypeIndexOf(causeStack, '\n at');
13081314
if (causeStackStart !== -1) {
13091315
const causeFrames = StringPrototypeSplit(StringPrototypeSlice(causeStack, causeStackStart + 1), '\n');
@@ -1324,6 +1330,7 @@ function improveStack(stack, constructor, name, tag) {
13241330
// for "regular errors" (errors that "look normal") for now.
13251331
let len = name.length;
13261332

1333+
console.error(name, typeof name)
13271334
if (typeof name !== 'string') {
13281335
stack = StringPrototypeReplace(
13291336
stack,
@@ -1425,8 +1432,8 @@ function safeGetCWD() {
14251432
}
14261433

14271434
function formatError(err, constructor, tag, ctx, keys) {
1428-
const name = err.name != null ? err.name : 'Error';
1429-
let stack = getStackString(err);
1435+
const name = err.name != null ? String(err.name) : 'Error';
1436+
let stack = getStackString(ctx, err);
14301437

14311438
removeDuplicateErrorKeys(ctx, keys, err, stack);
14321439

test/parallel/test-util-inspect.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
777777
[undefined, 'RangeError: foo', '[RangeError: foo]'],
778778
[false, 'false [RangeError]: foo', '[RangeError: foo]'],
779779
['', 'foo', '[RangeError: foo]'],
780-
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[1,2,3]'],
780+
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[[\n 1,\n 2,\n 3\n]]'],
781781
].forEach(([value, outputStart, stack]) => {
782782
let err = new RangeError('foo');
783783
err.name = value;
@@ -3448,3 +3448,13 @@ assert.strictEqual(
34483448
${error.stack.split('\n').slice(1).join('\n')}`,
34493449
);
34503450
}
3451+
3452+
{
3453+
const error = new Error();
3454+
error.stack = [Symbol('foo')];
3455+
3456+
assert.strictEqual(
3457+
inspect(error),
3458+
'[[\n Symbol(foo)\n]]'
3459+
);
3460+
}

0 commit comments

Comments
 (0)