Skip to content

Commit db648b9

Browse files
SamVerschuerenaduh95
authored andcommitted
util: inspect: do not crash on an Error stack pointing to itself
PR-URL: #58196 Reviewed-By: Jordan Harband <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Ethan Arrowood <[email protected]>
1 parent a029a06 commit db648b9

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

lib/internal/util/inspect.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,12 @@ function getStackString(ctx, error) {
13191319
if (typeof error.stack === 'string') {
13201320
return error.stack;
13211321
}
1322-
return formatValue(ctx, error.stack);
1322+
ctx.seen.push(error);
1323+
ctx.indentationLvl += 4;
1324+
const result = formatValue(ctx, error.stack);
1325+
ctx.indentationLvl -= 4;
1326+
ctx.seen.pop();
1327+
return `${ErrorPrototypeToString(error)}\n ${result}`;
13231328
}
13241329
return ErrorPrototypeToString(error);
13251330
}

test/parallel/test-util-inspect.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,14 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
770770
// Note: Symbols are not supported by `Error#toString()` which is called by
771771
// accessing the `stack` property.
772772
[
773-
[404, '404 [RangeError]: foo', '[404]'],
773+
[404, '404 [RangeError]: foo', '[RangeError: foo\n 404]'],
774774
[0, '0 [RangeError]: foo', '[RangeError: foo]'],
775775
[0n, '0 [RangeError]: foo', '[RangeError: foo]'],
776776
[null, 'null: foo', '[RangeError: foo]'],
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', '[[\n 1,\n 2,\n 3\n]]'],
780+
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[RangeError: foo\n [\n 1,\n 2,\n 3\n ]]'],
781781
].forEach(([value, outputStart, stack]) => {
782782
let err = new RangeError('foo');
783783
err.name = value;
@@ -3425,6 +3425,34 @@ ${error.stack.split('\n').slice(1).join('\n')}`,
34253425

34263426
assert.strictEqual(
34273427
inspect(error),
3428-
'[[\n Symbol(foo)\n]]'
3428+
'[Error\n [\n Symbol(foo)\n ]]'
34293429
);
34303430
}
3431+
3432+
{
3433+
const prepareStackTrace = Error.prepareStackTrace;
3434+
3435+
Error.prepareStackTrace = (error) => error;
3436+
3437+
const error = new Error('foo');
3438+
3439+
assert.strictEqual(inspect(error), '[Error: foo\n [Circular *1]]');
3440+
3441+
Error.prepareStackTrace = prepareStackTrace;
3442+
}
3443+
3444+
{
3445+
const error = new Error('foo');
3446+
error.stack = error;
3447+
3448+
assert.strictEqual(inspect(error), '[Error: foo\n [Circular *1]]');
3449+
}
3450+
3451+
{
3452+
const error = new Error('foo');
3453+
const error2 = new Error('bar');
3454+
error.stack = error2;
3455+
error2.stack = error;
3456+
3457+
assert.strictEqual(inspect(error), '[Error: foo\n [Error: bar\n [Circular *1]]]');
3458+
}

0 commit comments

Comments
 (0)