Skip to content

Commit

Permalink
util: inspect: do not crash on an Error stack that contains a Symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 12, 2025
1 parent 4c27393 commit 0a8293c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1285,8 +1285,14 @@ function identicalSequenceRange(a, b) {
return { len: 0, offset: 0 };
}

function getStackString(error) {
return error.stack ? String(error.stack) : ErrorPrototypeToString(error);
function getStackString(ctx, error) {
if (error.stack) {
if (ArrayIsArray(error.stack)) {
return formatArray(ctx, error.stack);
}
return String(error.stack);
}
return ErrorPrototypeToString(error);
}

function getStackFrames(ctx, err, stack) {
Expand All @@ -1301,7 +1307,7 @@ function getStackFrames(ctx, err, stack) {

// Remove stack frames identical to frames in cause.
if (cause != null && isError(cause)) {
const causeStack = getStackString(cause);
const causeStack = getStackString(ctx, cause);
const causeStackStart = StringPrototypeIndexOf(causeStack, '\n at');
if (causeStackStart !== -1) {
const causeFrames = StringPrototypeSplit(StringPrototypeSlice(causeStack, causeStackStart + 1), '\n');
Expand Down Expand Up @@ -1415,7 +1421,7 @@ function safeGetCWD() {

function formatError(err, constructor, tag, ctx, keys) {
const name = err.name != null ? String(err.name) : 'Error';
let stack = getStackString(err);
let stack = getStackString(ctx, err);

removeDuplicateErrorKeys(ctx, keys, err, stack);

Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3426,3 +3426,13 @@ assert.strictEqual(
Object.defineProperty(BuiltinPrototype, 'constructor', desc);
}
}

{
const error = new Error();
error.stack = [Symbol('foo')];

assert.strictEqual(
inspect(error),
'[Symbol(foo)]'
);
}

0 comments on commit 0a8293c

Please sign in to comment.