diff --git a/helix-core/src/surround.rs b/helix-core/src/surround.rs
index 513f8749..ed976488 100644
--- a/helix-core/src/surround.rs
+++ b/helix-core/src/surround.rs
@@ -167,6 +167,10 @@ fn find_nth_open_pair(
     mut pos: usize,
     n: usize,
 ) -> Option<usize> {
+    if pos >= text.len_chars() {
+        return None;
+    }
+
     let mut chars = text.chars_at(pos + 1);
 
     // Adjusts pos for the first iteration, and handles the case of the
@@ -383,6 +387,21 @@ mod test {
         )
     }
 
+    #[test]
+    fn test_find_nth_closest_pairs_pos_index_range_panic() {
+        #[rustfmt::skip]
+        let (doc, selection, _) =
+            rope_with_selections_and_expectations(
+                "(a)c)",
+                "^^^^^"
+            );
+
+        assert_eq!(
+            find_nth_closest_pairs_pos(doc.slice(..), selection.primary(), 1),
+            Err(Error::PairNotFound)
+        )
+    }
+
     // Create a Rope and a matching Selection using a specification language.
     // ^ is a single-point selection.
     // _ is an expected index. These are returned as a Vec<usize> for use in assertions.