Skip to content

Commit 7cc979d

Browse files
BridgeARtargos
authored andcommitted
util: recover from maximum call stack size
Using util.inspect should still return values in case the maximum call stack size is reached. This is important to inspect linked lists and similar. PR-URL: #20725 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ba8ee17 commit 7cc979d

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

doc/api/util.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ stream.write('With ES6');
360360
<!-- YAML
361361
added: v0.3.0
362362
changes:
363+
- version: REPLACEME
364+
pr-url: https://github.com/nodejs/node/pull/REPLACEME
365+
description: Inspecting linked lists and similar objects is now possible
366+
up to the maximum call stack size.
363367
- version: v10.0.0
364368
pr-url: https://github.com/nodejs/node/pull/19259
365369
description: The `WeakMap` and `WeakSet` entries can now be inspected.
@@ -387,8 +391,9 @@ changes:
387391
properties will be included in the formatted result as well as [`WeakMap`][]
388392
and [`WeakSet`][] entries. **Default:** `false`.
389393
* `depth` {number} Specifies the number of times to recurse while formatting
390-
the `object`. This is useful for inspecting large complicated objects.
391-
To make it recurse indefinitely pass `null`. **Default:** `2`.
394+
the `object`. This is useful for inspecting large complicated objects. To
395+
make it recurse up to the maximum call stack size pass `Infinity` or `null`.
396+
**Default:** `2`.
392397
* `colors` {boolean} If `true`, the output will be styled with ANSI color
393398
codes. Colors are customizable, see [Customizing `util.inspect` colors][].
394399
**Default:** `false`.

lib/util.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -711,14 +711,32 @@ function formatValue(ctx, value, recurseTimes) {
711711
}
712712

713713
ctx.seen.push(value);
714-
const output = formatter(ctx, value, recurseTimes, keys);
715-
714+
let output;
715+
// This corresponds to a depth of at least 333 and likely 500.
716+
if (ctx.indentationLvl < 1000) {
717+
output = formatter(ctx, value, recurseTimes, keys);
718+
} else {
719+
try {
720+
output = formatter(ctx, value, recurseTimes, keys);
721+
} catch (err) {
722+
if (errors.isStackOverflowError(err)) {
723+
ctx.seen.pop();
724+
return ctx.stylize(
725+
`[${constructor || tag || 'Object'}: Inspection interrupted ` +
726+
'prematurely. Maximum call stack size exceeded.]',
727+
'special'
728+
);
729+
}
730+
throw err;
731+
}
732+
}
716733
if (extra !== undefined)
717734
output.unshift(extra);
718735

719736
for (var i = 0; i < symbols.length; i++) {
720737
output.push(formatProperty(ctx, value, recurseTimes, symbols[i], 0));
721738
}
739+
722740
ctx.seen.pop();
723741

724742
return reduceToSingleString(ctx, output, base, braces);

test/parallel/test-util-inspect.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1459,10 +1459,17 @@ util.inspect(process);
14591459
// Test that a long linked list can be inspected without throwing an error.
14601460
const list = {};
14611461
let head = list;
1462-
// The real cutoff value is closer to 1400 stack frames as of May 2018,
1463-
// but let's be generous here – even a linked listed of length 100k should be
1464-
// inspectable in some way.
1462+
// A linked list of length 100k should be inspectable in some way, even though
1463+
// the real cutoff value is much lower than 100k.
14651464
for (let i = 0; i < 100000; i++)
14661465
head = head.next = {};
1467-
util.inspect(list);
1466+
assert.strictEqual(
1467+
util.inspect(list),
1468+
'{ next: { next: { next: [Object] } } }'
1469+
);
1470+
const longList = util.inspect(list, { depth: Infinity });
1471+
const match = longList.match(/next/g);
1472+
assert(match.length > 1000 && match.length < 10000);
1473+
assert(longList.includes('[Object: Inspection interrupted ' +
1474+
'prematurely. Maximum call stack size exceeded.]'));
14681475
}

0 commit comments

Comments
 (0)