Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a9d332a

Browse filesBrowse files
ljharbaduh95
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 <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jason Zhang <xzha4350@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 649cf0c commit a9d332a
Copy full SHA for a9d332a

File tree

Expand file treeCollapse file tree

2 files changed

+26
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+26
-8
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+10-4Lines changed: 10 additions & 4 deletions
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

Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+16-4Lines changed: 16 additions & 4 deletions
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

@@ -3416,3 +3418,13 @@ assert.strictEqual(
34163418
${error.stack.split('\n').slice(1).join('\n')}`,
34173419
);
34183420
}
3421+
3422+
{
3423+
const error = new Error();
3424+
error.stack = [Symbol('foo')];
3425+
3426+
assert.strictEqual(
3427+
inspect(error),
3428+
'[[\n Symbol(foo)\n]]'
3429+
);
3430+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.