From 8b819f9240e12d3a07fb334085222008e99b87d1 Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Tue, 6 May 2025 17:57:24 +0200 Subject: [PATCH 1/2] util: inspect: do not crash on an Error stack pointing to itself --- lib/internal/util/inspect.js | 4 +++- test/parallel/test-util-inspect.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 375a0091209161..4b4ab4b162e46d 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1319,7 +1319,9 @@ function getStackString(ctx, error) { if (typeof error.stack === 'string') { return error.stack; } - return formatValue(ctx, error.stack); + if (error.stack !== error) { + return formatValue(ctx, error.stack); + } } return ErrorPrototypeToString(error); } diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 2dc263443481a0..bf29205e0506b2 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -3460,3 +3460,22 @@ ${error.stack.split('\n').slice(1).join('\n')}`, '[[\n Symbol(foo)\n]]' ); } + +{ + const prepareStackTrace = Error.prepareStackTrace; + + Error.prepareStackTrace = (error) => error; + + const error = new Error('foo'); + + assert.strictEqual(inspect(error), '[Error: foo]'); + + Error.prepareStackTrace = prepareStackTrace; +} + +{ + const error = new Error('foo'); + error.stack = error + + assert.strictEqual(inspect(error), '[Error: foo]'); +} From f616bccab6e8a565a8d8c41cb5e9be25b8ad1023 Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Tue, 6 May 2025 18:07:38 +0200 Subject: [PATCH 2/2] fix: linting error --- test/parallel/test-util-inspect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index bf29205e0506b2..d28f97eda2eb89 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -3475,7 +3475,7 @@ ${error.stack.split('\n').slice(1).join('\n')}`, { const error = new Error('foo'); - error.stack = error + error.stack = error; assert.strictEqual(inspect(error), '[Error: foo]'); }