Skip to content

Commit 19fabc0

Browse files
ljharbjasnell
authored andcommitted
util: inspect: do not crash on an Error stack that contains a Symbol
See #56570 PR-URL: #56573 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Jason Zhang <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 1921371 commit 19fabc0

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

lib/internal/util/inspect.js

+10-4
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');
@@ -1426,7 +1432,7 @@ function safeGetCWD() {
14261432

14271433
function formatError(err, constructor, tag, ctx, keys) {
14281434
const name = err.name != null ? err.name : 'Error';
1429-
let stack = getStackString(err);
1435+
let stack = getStackString(ctx, err);
14301436

14311437
removeDuplicateErrorKeys(ctx, keys, err, stack);
14321438

test/parallel/test-util-inspect.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -777,16 +777,18 @@ 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;
784+
const result = util.inspect(err);
784785
assert(
785-
util.inspect(err).startsWith(outputStart),
786+
result.startsWith(outputStart),
786787
util.format(
787-
'The name set to %o did not result in the expected output "%s"',
788+
'The name set to %o did not result in the expected output "%s", got "%s"',
788789
value,
789-
outputStart
790+
outputStart,
791+
result.split('\n')[0]
790792
)
791793
);
792794

@@ -3448,3 +3450,13 @@ assert.strictEqual(
34483450
${error.stack.split('\n').slice(1).join('\n')}`,
34493451
);
34503452
}
3453+
3454+
{
3455+
const error = new Error();
3456+
error.stack = [Symbol('foo')];
3457+
3458+
assert.strictEqual(
3459+
inspect(error),
3460+
'[[\n Symbol(foo)\n]]'
3461+
);
3462+
}

0 commit comments

Comments
 (0)