Skip to content

Commit ee66a38

Browse files
Ijtihedaduh95
authored andcommitted
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 5b26439 commit ee66a38

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
@@ -1604,9 +1604,15 @@ function markNodeModules(ctx, line) {
16041604
tempLine+=StringPrototypeSlice(line,lastPos,moduleStart);
16051605

16061606
letmoduleEnd=StringPrototypeIndexOf(line,separator,moduleStart);
1607-
if(line[moduleStart]==='@'){
1607+
if(moduleEnd===-1){
1608+
// No trailing separator: the module name runs to the end of the line.
1609+
moduleEnd=line.length;
1610+
}elseif(line[moduleStart]==='@'){
16081611
// Namespaced modules have an extra slash: @namespace/package
16091612
moduleEnd=StringPrototypeIndexOf(line,separator,moduleEnd+1);
1613+
if(moduleEnd===-1){
1614+
moduleEnd=line.length;
1615+
}
16101616
}
16111617

16121618
constnodeModule=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+
consterr=newError('boom');
3427+
err.stack='Error: boom\n at /app/node_modules/foo.js:1:1';
3428+
constout=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
consterr=newError('foo');

0 commit comments

Comments
 (0)