Skip to content

Commit 8b819f9

Browse files
util: inspect: do not crash on an Error stack pointing to itself
1 parent e272637 commit 8b819f9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/internal/util/inspect.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,9 @@ function getStackString(ctx, error) {
13191319
if (typeof error.stack === 'string') {
13201320
return error.stack;
13211321
}
1322-
return formatValue(ctx, error.stack);
1322+
if (error.stack !== error) {
1323+
return formatValue(ctx, error.stack);
1324+
}
13231325
}
13241326
return ErrorPrototypeToString(error);
13251327
}

test/parallel/test-util-inspect.js

+19
Original file line numberDiff line numberDiff line change
@@ -3460,3 +3460,22 @@ ${error.stack.split('\n').slice(1).join('\n')}`,
34603460
'[[\n Symbol(foo)\n]]'
34613461
);
34623462
}
3463+
3464+
{
3465+
const prepareStackTrace = Error.prepareStackTrace;
3466+
3467+
Error.prepareStackTrace = (error) => error;
3468+
3469+
const error = new Error('foo');
3470+
3471+
assert.strictEqual(inspect(error), '[Error: foo]');
3472+
3473+
Error.prepareStackTrace = prepareStackTrace;
3474+
}
3475+
3476+
{
3477+
const error = new Error('foo');
3478+
error.stack = error
3479+
3480+
assert.strictEqual(inspect(error), '[Error: foo]');
3481+
}

0 commit comments

Comments
 (0)