Skip to content

util: fix OOM in inspect color stack formatting#64022

Open
Ijtihed wants to merge 1 commit into
nodejs:mainfrom
Ijtihed:fix-inspect-oom-64011
Open

util: fix OOM in inspect color stack formatting#64022
Ijtihed wants to merge 1 commit into
nodejs:mainfrom
Ijtihed:fix-inspect-oom-64011

Conversation

@Ijtihed

@Ijtihed Ijtihed commented Jun 20, 2026

Copy link
Copy Markdown

Fixes the heap out-of-memory crash in util.inspect(err, { colors: true }) reported in #64011. When err.stack contained a frame whose node_modules path segment was the final path component with no trailing separator (for example at /app/node_modules/foo.js:1:1), markNodeModules entered an infinite loop so the StringPrototypeIndexOf search for the next separator returned -1 moduleEnd and searchFrom were set to -1 and the next StringPrototypeIndexOf(line, 'node_modules', -1) restarted the scan from index 0 which rematched the same segment and growing tempLine without bound until the heap was exhausted.

The fix clamps moduleEnd to line.length when no trailing separator is found so searchFrom advances past the match and the loop terminates. It also guards the namespaced (@scope) branch against running on a -1 indexd which closed a second path into the same loop.

Adds a regression test in test/parallel/test-util-inspect.js which asserrts that inspecting an error with such a stack frame returns the expected colorized output instead of hanging

Fixes #64011

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Jun 20, 2026

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 20, 2026
@Ijtihed Ijtihed force-pushed the fix-inspect-oom-64011 branch from 712033b to c824e6e Compare June 20, 2026 09:23
@Ijtihed

Ijtihed commented Jun 20, 2026

Copy link
Copy Markdown
Author

failed the Signed-off-by stuff. could you rerun ci? @mcollina

@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 20, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@BridgeAR BridgeAR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix!

// Namespaced modules have an extra slash: @namespace/package
moduleEnd = StringPrototypeIndexOf(line, separator, moduleEnd + 1);
}
if (moduleEnd === -1) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nit: we could make it a nested if statement to have one less comparison

@Ijtihed Ijtihed Jun 20, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks!

@Ijtihed Ijtihed force-pushed the fix-inspect-oom-64011 branch from c824e6e to b478916 Compare June 20, 2026 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

util.inspect color OOM crash

4 participants