fix(rg): avoid eager rg allocations in low-output/search-json paths#1690
Merged
chaliy merged 2 commits intoMay 24, 2026
Merged
Conversation
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
bashkit | 051f43b | Commit Preview URL | May 24 2026, 06:52 PM |
1ddd4db to
5a83124
Compare
write_rg_json_match previously collected all per-line submatches into a Vec<serde_json::Value> before serializing the whole tree, which on attacker-controlled lines with many matches allocated proportional to matches_per_line before the bytewise write to the output buffer. Stream the JSON header to the output, then emit each submatch object as its own serialized string between brackets. The over-the-wire output is byte-identical to the previous implementation; only the intermediate Vec<Value> allocation is eliminated. Preserves a defensive fallback to the old single-value path if the header serializer ever produces an unexpected shape.
7f3a3af to
82352a7
Compare
The streaming write_rg_json_match used strip_suffix("}}") to splice
submatches into the serialized header. But serde_json::Object keys serialize
in lexicographic order, so the JSON ends with `"type":"match"}` — only one
trailing `}` — and the strip silently falls through to the defensive
fallback that emits no submatches at all, breaking JSON-output parity
with real ripgrep.
Serialize the inner `data` object directly, strip its single trailing
`}`, append `"submatches":[...]` (which sorts last alphabetically after
`path`), then close `data` and emit `,"type":"match"}` to wrap the
envelope. Same streaming/no-allocation property as before, now with
correct output.
Regression covered by the existing differential test
test_rg_matches_real_rg_cases (case "json basic"), which was failing on
the previous strip_suffix("}}") path and now passes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
rgeagerly built per-line and per-match vectors, allowing attacker-controlled files to exhaust memory before output caps applied.Description
Vec<RgLine>when an output mode requires random-access line metadata (JSON, passthru, vimgrep, only-matching output, context, or non-count outputs).split_rg_linesusage with a streaming line scan (split_inclusive(' ')+scan) for match detection and counting in low-output modes.Vec<serde_json::Value>for JSON submatches by streaming submatch serialization directly into the output buffer inwrite_rg_json_match.Testing
cargo fmtand formatting check succeeded.cargo test -p bashkit builtins::rg::tests:: -- --nocaptureand the rg test cases passed (30 passed; 0 failed).Codex Task