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
See #56570
  • Loading branch information
ljharb committed Jan 12, 2025
1 parent bf59539 commit 2e0f183
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1287,8 +1287,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 (typeof error.stack === 'string') {
return error.stack;
}
return formatValue(ctx, error.stack);
}
return ErrorPrototypeToString(error);
}

function getStackFrames(ctx, err, stack) {
Expand All @@ -1303,7 +1309,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 All @@ -1324,6 +1330,7 @@ function improveStack(stack, constructor, name, tag) {
// for "regular errors" (errors that "look normal") for now.
let len = name.length;

console.error(name, typeof name)

Check failure on line 1333 in lib/internal/util/inspect.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected use of 'console'. Use `const console = require('internal/console/global');` instead of the global

Check failure on line 1333 in lib/internal/util/inspect.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'console' is not defined

Check failure on line 1333 in lib/internal/util/inspect.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
if (typeof name !== 'string') {
stack = StringPrototypeReplace(
stack,
Expand Down Expand Up @@ -1425,8 +1432,8 @@ function safeGetCWD() {
}

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

removeDuplicateErrorKeys(ctx, keys, err, stack);

Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
[undefined, 'RangeError: foo', '[RangeError: foo]'],
[false, 'false [RangeError]: foo', '[RangeError: foo]'],
['', 'foo', '[RangeError: foo]'],
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[1,2,3]'],
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[[\n 1,\n 2,\n 3\n]]'],
].forEach(([value, outputStart, stack]) => {
let err = new RangeError('foo');
err.name = value;
Expand Down Expand Up @@ -3448,3 +3448,13 @@ assert.strictEqual(
${error.stack.split('\n').slice(1).join('\n')}`,
);
}

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

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

0 comments on commit 2e0f183

Please sign in to comment.