Repository navigation
fix: panic on an empty content match at end of line - #1201
Merged
Merged
Conversation
The left-trimming loop indexed hay before checking the bound, so an empty match at pos == hay.len() read out of bounds. Searching cr/$/ in a file with CRLF endings killed broot with exit 101.
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.
Searching
cr/$/in a directory containing a file with CRLF line endings kills broot./tmp/brepro2withcrlf.txt(hello\r\nworld\r\n) andtabend.txt(abc\t\n), driven through the real TUI under tmux, typingcr/$/:Cause
The left-trimming loop tests the two conditions in the wrong order:
hay[extract_start]is evaluated before the bound is checked. An empty match at the very end of a line givespos == hay.len(), so the index read is out of range.$on a line ending in a control character (the\rof CRLF, or a trailing tab) is a natural way to produce that.The fix
Swap the two operands so the bound is checked first. That is the whole change;
&&short-circuits, so nothing else needs to move.Verification
Three tests in
content_match.rs: the empty-match-at-end regression, plus two that pin the existing trimming behaviour (leading spaces are still dropped, and trimming still stops at the needle) so the reorder cannot quietly change what the function does.Reverting only the condition order fails the regression test with the original panic:
cargo testis 59 + 7 passed,cargo clippy --all-targetsis clean.cargo fmt --checkreports pre-existing diffs elsewhere in the repo (rustfmt.toml uses nightly-only options), but none in the file I touched.Disclosure: written with AI assistance (Claude Code). I reproduced the panic in a real terminal against a binary built from master, confirmed it is gone on the fixed binary, and ran the mutation check myself.