Skip to content

Commit

Permalink
util: do not catch on circular @@toStringTag errors
Browse files Browse the repository at this point in the history
PR-URL: #55544
Fixes: #55539
Reviewed-By: James M Snell <[email protected]>
Co-Authored-By: Colin Ihrig <[email protected]>
  • Loading branch information
2 people authored and ruyadorno committed Nov 27, 2024
1 parent cad55da commit f12e596
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,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);
}
Expand Down Expand Up @@ -1547,17 +1548,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) {
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f12e596

Please sign in to comment.