Skip to content

Commit e27b047

Browse files
authored
Fix panic in select_textobject_around (#9832)
Test Document ------------- ``` a)b ``` Steps to Reproduce ------------------ 1. % # select_all 1. ms( # surround_add 1. mam # select_textobject_around Debug and Release ----------------- `thread 'main' panicked at 'Attempt to index past end of RopeSlice: char index 7, RopeSlice char length 6', ropey-1.6.1/src/slice.rs:796:13` Description ----------- An index was selected beyond the end of the slice with chars_at. The fix adds a guard check to `find_nth_open_pair`, like in the other find_nth* functions.
1 parent cb01e52 commit e27b047

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

helix-core/src/surround.rs

+19
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ fn find_nth_open_pair(
167167
mut pos: usize,
168168
n: usize,
169169
) -> Option<usize> {
170+
if pos >= text.len_chars() {
171+
return None;
172+
}
173+
170174
let mut chars = text.chars_at(pos + 1);
171175

172176
// Adjusts pos for the first iteration, and handles the case of the
@@ -383,6 +387,21 @@ mod test {
383387
)
384388
}
385389

390+
#[test]
391+
fn test_find_nth_closest_pairs_pos_index_range_panic() {
392+
#[rustfmt::skip]
393+
let (doc, selection, _) =
394+
rope_with_selections_and_expectations(
395+
"(a)c)",
396+
"^^^^^"
397+
);
398+
399+
assert_eq!(
400+
find_nth_closest_pairs_pos(doc.slice(..), selection.primary(), 1),
401+
Err(Error::PairNotFound)
402+
)
403+
}
404+
386405
// Create a Rope and a matching Selection using a specification language.
387406
// ^ is a single-point selection.
388407
// _ is an expected index. These are returned as a Vec<usize> for use in assertions.

0 commit comments

Comments
 (0)