Skip to content

Commit 0a8293c

Browse files
committed
util: inspect: do not crash on an Error stack that contains a Symbol
See #56570
1 parent 4c27393 commit 0a8293c

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
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 (ArrayIsArray(error.stack)) {
1291+
return formatArray(ctx, error.stack);
1292+
}
1293+
return String(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

+10
Original file line numberDiff line numberDiff line change
@@ -3426,3 +3426,13 @@ assert.strictEqual(
34263426
Object.defineProperty(BuiltinPrototype, 'constructor', desc);
34273427
}
34283428
}
3429+
3430+
{
3431+
const error = new Error();
3432+
error.stack = [Symbol('foo')];
3433+
3434+
assert.strictEqual(
3435+
inspect(error),
3436+
'[Symbol(foo)]'
3437+
);
3438+
}

0 commit comments

Comments
 (0)