Skip to content

Commit 8ed3e8b

Browse files
committed
util: inspect: do not crash on an Error stack that contains a Symbol
See nodejs#56570
1 parent 2570f95 commit 8ed3e8b

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/internal/util/inspect.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,14 @@ function identicalSequenceRange(a, b) {
12851285
return { len: 0, offset: 0 };
12861286
}
12871287

1288-
function getStackString(error) {
1289-
return error.stack ? String(error.stack) : ErrorPrototypeToString(error);
1288+
function getStackString(ctx, error) {
1289+
if (error.stack) {
1290+
if (typeof error.stack === 'string') {
1291+
return error.stack;
1292+
}
1293+
return formatValue(ctx, error.stack);
1294+
}
1295+
return ErrorPrototypeToString(error);
12901296
}
12911297

12921298
function getStackFrames(ctx, err, stack) {
@@ -1301,7 +1307,7 @@ function getStackFrames(ctx, err, stack) {
13011307

13021308
// Remove stack frames identical to frames in cause.
13031309
if (cause != null && isError(cause)) {
1304-
const causeStack = getStackString(cause);
1310+
const causeStack = getStackString(ctx, cause);
13051311
const causeStackStart = StringPrototypeIndexOf(causeStack, '\n at');
13061312
if (causeStackStart !== -1) {
13071313
const causeFrames = StringPrototypeSplit(StringPrototypeSlice(causeStack, causeStackStart + 1), '\n');
@@ -1415,7 +1421,7 @@ function safeGetCWD() {
14151421

14161422
function formatError(err, constructor, tag, ctx, keys) {
14171423
const name = err.name != null ? String(err.name) : 'Error';
1418-
let stack = getStackString(err);
1424+
let stack = getStackString(ctx, err);
14191425

14201426
removeDuplicateErrorKeys(ctx, keys, err, stack);
14211427

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: foo', '[RangeError: foo]'],
779779
['', 'foo', '[RangeError: foo]'],
780-
[[1, 2, 3], '1,2,3: foo', '[1,2,3]'],
780+
[[1, 2, 3], '1,2,3: foo', '[[\n 1,\n 2,\n 3\n]]'],
781781
].forEach(([value, outputStart, stack]) => {
782782
let err = new RangeError('foo');
783783
err.name = value;
@@ -3436,3 +3436,13 @@ assert.strictEqual(
34363436
'[Function: Symbol(f)]',
34373437
);
34383438
}
3439+
3440+
{
3441+
const error = new Error();
3442+
error.stack = [Symbol('foo')];
3443+
3444+
assert.strictEqual(
3445+
inspect(error),
3446+
'[[\n Symbol(foo)\n]]'
3447+
);
3448+
}

0 commit comments

Comments
 (0)