Skip to content

Commit

Permalink
util: don't catch on circular toStringTag error
Browse files Browse the repository at this point in the history
Co-Authored-By: Colin Ihrig <[email protected]>
  • Loading branch information
RedYetiDev and cjihrig committed Oct 26, 2024
1 parent 53b1050 commit e1669f2
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 @@ -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);
}
Expand Down Expand Up @@ -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) {
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 e1669f2

Please sign in to comment.