From e2450347f17835292c1aaf7e8546c89cf9a4fbc1 Mon Sep 17 00:00:00 2001 From: Ijtihed Kilani Date: Sat, 20 Jun 2026 09:44:21 +0300 Subject: [PATCH] util: fix OOM in inspect color stack formatting Signed-off-by: Ijtihed Kilani --- lib/internal/util/inspect.js | 8 +++++++- test/parallel/test-util-inspect.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index dd8423b3c9e4f1..c959c056fece45 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1876,9 +1876,15 @@ function markNodeModules(ctx, line) { tempLine += StringPrototypeSlice(line, lastPos, moduleStart); let moduleEnd = StringPrototypeIndexOf(line, separator, moduleStart); - if (line[moduleStart] === '@') { + if (moduleEnd === -1) { + // No trailing separator: the module name runs to the end of the line. + moduleEnd = line.length; + } else if (line[moduleStart] === '@') { // Namespaced modules have an extra slash: @namespace/package moduleEnd = StringPrototypeIndexOf(line, separator, moduleEnd + 1); + if (moduleEnd === -1) { + moduleEnd = line.length; + } } const nodeModule = StringPrototypeSlice(line, moduleStart, moduleEnd); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index c41c89c7efa113..6a1da6d4129fbd 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -3418,6 +3418,21 @@ assert.strictEqual( ); } +{ + // A node_modules segment that is the last path component (no trailing + // separator after the module name) must not send markNodeModules into an + // infinite loop that exhausts the heap. + // https://github.com/nodejs/node/issues/64011 + const err = new Error('boom'); + err.stack = 'Error: boom\n at /app/node_modules/foo.js:1:1'; + const out = util.inspect(err, { colors: true }); + assert.strictEqual( + out, + 'Error: boom\n' + + ' at /app/node_modules/\x1B[4mfoo.js:1:1\x1B[24m', + ); +} + { // Cross platform checks. const err = new Error('foo');