Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- fix preview transformers extension matching not working with double extensions such as `.tar.gz` - Fix #1195
- strip escape sequences from displayed names to prevent OSC injections - Fix #1188 - Thanks @carfeii
- fall back to numeric uid/gid instead of `????` when the user or group name can't be resolved, which is always the case on statically linked musl builds - Fix #1075
- fix panic on a content regex matching the empty string at the end of a line ending with a control char (eg `cr/$/` on a CRLF file)

### v1.58.0 - 2026-07-10
<a name="v1.58.0"></a>
Expand Down
44 changes: 43 additions & 1 deletion src/content_search/content_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl ContentMatch {
extract_start -= 1;
}
// left trimming
while (hay[extract_start] == 32) && extract_start < pos {
while extract_start < pos && hay[extract_start] == 32 {
extract_start += 1;
}
loop {
Expand All @@ -59,3 +59,45 @@ impl ContentMatch {
}
}
}

#[cfg(test)]
mod content_match_tests {
use super::*;

/// Regression: an empty match at the very end of a line (eg searching
/// `cr/$/` in a file whose line ends with a control char, such as a tab
/// or the `\r` of a CRLF line ending) gives `pos == hay.len()`. The left
/// trimming loop used to read `hay[extract_start]` before checking
/// `extract_start < pos`, panicking with "index out of bounds".
#[test]
fn empty_match_at_end_of_line_doesnt_panic() {
// line ending with a tab: the extract_start loop stops on the control
// char, leaving extract_start == pos == hay.len()
let m = ContentMatch::build(b"abc\t", 4, "", 30);
assert_eq!(m.extract, "");
assert_eq!(m.needle_start, 0);
assert_eq!(m.needle_end, 0);
// same with the `\r` of a CRLF line ending
let m = ContentMatch::build(b"hello\r", 6, "", 30);
assert_eq!(m.extract, "");
}

/// The left trimming must still drop the spaces before the needle.
#[test]
fn left_trimming_still_drops_leading_spaces() {
let m = ContentMatch::build(b"\t needle here", 7, "needle", 30);
assert_eq!(m.extract, "needle here");
assert_eq!(m.needle_start, 0);
assert_eq!(m.needle_end, 6);
}

/// Trimming must not eat the needle itself when the needle starts with a
/// space: it stops at `pos`.
#[test]
fn left_trimming_stops_at_needle() {
// bytes: \t=0 ' '=1 ' '=2 'a'=3 ' '=4 'x'=5, needle " a" at pos 2
let m = ContentMatch::build(b"\t a x", 2, " a", 30);
assert_eq!(m.extract, " a x");
assert_eq!(m.needle_start, 0);
}
}
Loading