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 <jasnell@gmail.com>
Co-Authored-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Aviv Keller 2024-11-06 04:57:15 -05:00 committed by GitHub
parent 98e5693cd4
commit c185e11623
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 11 deletions

View File

@ -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) {

View File

@ -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;