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

This comment has been minimized.

@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
@Ijtihed

Ijtihed commented Jun 21, 2026

Copy link
Copy Markdown
Author

Thanks! Anything else from my side?

@Renegade334 Renegade334 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Jun 21, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 21, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@mcollina

Copy link
Copy Markdown
Member

Thanks! Anything else from my side?

Due to some regressions, you'd need to rebase

Signed-off-by: Ijtihed Kilani <ijtihedk@gmail.com>
@Ijtihed Ijtihed force-pushed the fix-inspect-oom-64011 branch from b478916 to e245034 Compare June 21, 2026 18:05
@Ijtihed

Ijtihed commented Jun 21, 2026

Copy link
Copy Markdown
Author

Thanks! Anything else from my side?

Due to some regressions, you'd need to rebase

Done

@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 21, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 21, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. 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

6 participants