From c185e1162385ecd23ae2eea2fd38510ad0b6a673 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Wed, 6 Nov 2024 04:57:15 -0500 Subject: [PATCH] util: do not catch on circular `@@toStringTag` errors PR-URL: https://github.com/nodejs/node/pull/55544 Fixes: https://github.com/nodejs/node/issues/55539 Reviewed-By: James M Snell Co-Authored-By: Colin Ihrig --- lib/internal/util/inspect.js | 19 ++++++++----------- test/parallel/test-util-inspect.js | 9 +++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 9480bcb2619..a697459468d 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1072,6 +1072,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { ArrayPrototypePushApply(output, protoProps); } } catch (err) { + if (!isStackOverflowError(err)) throw err; const constructorName = StringPrototypeSlice(getCtxStyle(value, constructor, tag), 0, -1); return handleMaxCallStackSize(ctx, err, constructorName, indentationLvl); } @@ -1557,17 +1558,13 @@ function groupArrayElements(ctx, output, value) { } function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) { - if (isStackOverflowError(err)) { - ctx.seen.pop(); - ctx.indentationLvl = indentationLvl; - return ctx.stylize( - `[${constructorName}: Inspection interrupted ` + - 'prematurely. Maximum call stack size exceeded.]', - 'special', - ); - } - /* c8 ignore next */ - assert.fail(err.stack); + ctx.seen.pop(); + ctx.indentationLvl = indentationLvl; + return ctx.stylize( + `[${constructorName}: Inspection interrupted ` + + 'prematurely. Maximum call stack size exceeded.]', + 'special', + ); } function addNumericSeparator(integerString) { diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index b95d1c37847..3da292fc663 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1644,6 +1644,15 @@ util.inspect(process); assert.throws(() => util.inspect(new ThrowingClass()), /toStringTag error/); + const y = { + get [Symbol.toStringTag]() { + return JSON.stringify(this); + } + }; + const x = { y }; + y.x = x; + assert.throws(() => util.inspect(x), /TypeError: Converting circular structure to JSON/); + class NotStringClass { get [Symbol.toStringTag]() { return null;