Skip to content

Commit ff58f43

Browse files
authored
util: fix OOM in inspect color stack formatting
Signed-off-by: Ijtihed Kilani <ijtihedk@gmail.com> PR-URL: #64022 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 2989072 commit ff58f43

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/internal/util/inspect.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1876,9 +1876,15 @@ function markNodeModules(ctx, line) {
18761876
tempLine += StringPrototypeSlice(line, lastPos, moduleStart);
18771877

18781878
let moduleEnd = StringPrototypeIndexOf(line, separator, moduleStart);
1879-
if (line[moduleStart] === '@') {
1879+
if (moduleEnd === -1) {
1880+
// No trailing separator: the module name runs to the end of the line.
1881+
moduleEnd = line.length;
1882+
} else if (line[moduleStart] === '@') {
18801883
// Namespaced modules have an extra slash: @namespace/package
18811884
moduleEnd = StringPrototypeIndexOf(line, separator, moduleEnd + 1);
1885+
if (moduleEnd === -1) {
1886+
moduleEnd = line.length;
1887+
}
18821888
}
18831889

18841890
const nodeModule = StringPrototypeSlice(line, moduleStart, moduleEnd);

test/parallel/test-util-inspect.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,6 +3418,21 @@ assert.strictEqual(
34183418
);
34193419
}
34203420

3421+
{
3422+
// A node_modules segment that is the last path component (no trailing
3423+
// separator after the module name) must not send markNodeModules into an
3424+
// infinite loop that exhausts the heap.
3425+
// https://github.com/nodejs/node/issues/64011
3426+
const err = new Error('boom');
3427+
err.stack = 'Error: boom\n at /app/node_modules/foo.js:1:1';
3428+
const out = util.inspect(err, { colors: true });
3429+
assert.strictEqual(
3430+
out,
3431+
'Error: boom\n' +
3432+
' at /app/node_modules/\x1B[4mfoo.js:1:1\x1B[24m',
3433+
);
3434+
}
3435+
34213436
{
34223437
// Cross platform checks.
34233438
const err = new Error('foo');

0 commit comments

Comments
 (0)