Skip to content

fix(grep): collect large match sets without argument stack overflow - #1303

Open
kelpforge wants to merge 2 commits into
shelljs:mainfrom
kelpforge:fix-grep-many-groups
Open

kelpforge wants to merge 2 commits into
shelljs:mainfrom
kelpforge:fix-grep-many-groups

Conversation

@kelpforge

Copy link
Copy Markdown

ShellString('match\nother\n'.repeat(150000)).grep('match') throws Maximum call stack size exceeded on Node22.23.5/Linux. The grouped results are passed as individual arguments to push.apply, exceeding the runtime's argument limit.

Append each formatted group separately instead. The overlap check can reuse only the last group, so compare with the last group directly rather than scan every group with includes. This also removes quadratic membership scans for many separated matches. Context selection and result formatting are unchanged.

Added a150,000-separated-match regression that fails on the unmodified source with the stack overflow and passes with the fix. Added a context test covering coalesced overlapping windows and separated windows with line numbers.

Validation on Linux, Node22.23.5:

  • Focused grep suite:40 passed.
    -16,000 differential cases agree with base output across before/after context sizes, inverse matching and line numbers.
  • Full suite:657 passed,6 skipped,2 failures in cmd unknown-command message assertions. The same2 failures reproduce on the unchanged base in this environment. No full-suite green claim.
  • ESLint, Node-support consistency, generated docs (unchanged), and git diff --check pass.

This does not change the separate final-line/split-sentinel handling covered by #1268 and #1292.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant