Skip to content

Commit 42ac0c2

Browse files
avivkellercjihrig
authored andcommitted
util: do not catch on circular @@toStringTag errors
PR-URL: #55544 Fixes: #55539 Reviewed-By: James M Snell <jasnell@gmail.com> Co-Authored-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 83fb007 commit 42ac0c2

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

lib/internal/util/inspect.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
10621062
ArrayPrototypePushApply(output, protoProps);
10631063
}
10641064
} catch (err) {
1065+
if (!isStackOverflowError(err)) throw err;
10651066
const constructorName = StringPrototypeSlice(getCtxStyle(value, constructor, tag), 0, -1);
10661067
return handleMaxCallStackSize(ctx, err, constructorName, indentationLvl);
10671068
}
@@ -1547,17 +1548,13 @@ function groupArrayElements(ctx, output, value) {
15471548
}
15481549

15491550
function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
1550-
if (isStackOverflowError(err)) {
1551-
ctx.seen.pop();
1552-
ctx.indentationLvl = indentationLvl;
1553-
return ctx.stylize(
1554-
`[${constructorName}: Inspection interrupted ` +
1555-
'prematurely. Maximum call stack size exceeded.]',
1556-
'special',
1557-
);
1558-
}
1559-
/* c8 ignore next */
1560-
assert.fail(err.stack);
1551+
ctx.seen.pop();
1552+
ctx.indentationLvl = indentationLvl;
1553+
return ctx.stylize(
1554+
`[${constructorName}: Inspection interrupted ` +
1555+
'prematurely. Maximum call stack size exceeded.]',
1556+
'special',
1557+
);
15611558
}
15621559

15631560
function addNumericSeparator(integerString) {

test/parallel/test-util-inspect.js

+9
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,15 @@ util.inspect(process);
16441644

16451645
assert.throws(() => util.inspect(new ThrowingClass()), /toStringTag error/);
16461646

1647+
const y = {
1648+
get [Symbol.toStringTag]() {
1649+
return JSON.stringify(this);
1650+
}
1651+
};
1652+
const x = { y };
1653+
y.x = x;
1654+
assert.throws(() => util.inspect(x), /TypeError: Converting circular structure to JSON/);
1655+
16471656
class NotStringClass {
16481657
get [Symbol.toStringTag]() {
16491658
return null;

0 commit comments

Comments
 (0)