From 2b699053e67949dd4d12baefef20138c8d9ce4b4 Mon Sep 17 00:00:00 2001 From: Michael Sloan Date: Fri, 13 Dec 2024 11:16:30 -0700 Subject: [PATCH] Log invariant violations in fuzzy string match iterator (#21983) Seeing frequent inscrutable panics here Release Notes: - N/A --- Cargo.lock | 1 + crates/fuzzy/Cargo.toml | 1 + crates/fuzzy/src/strings.rs | 14 ++++++++++++++ 3 files changed, 16 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c60b84b877ba2b9dd2f8ba60f9e75356995c16b7..86bb0fc743927663d2e7216058bf22581cc981f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5030,6 +5030,7 @@ name = "fuzzy" version = "0.1.0" dependencies = [ "gpui", + "log", "util", ] diff --git a/crates/fuzzy/Cargo.toml b/crates/fuzzy/Cargo.toml index e3a016c98b718cc9852b9abece47213c284f8fcb..6d115fb1b5be8bdf5fdd9e4e9059ec49c8ba58b7 100644 --- a/crates/fuzzy/Cargo.toml +++ b/crates/fuzzy/Cargo.toml @@ -15,3 +15,4 @@ doctest = false [dependencies] gpui.workspace = true util.workspace = true +log.workspace = true diff --git a/crates/fuzzy/src/strings.rs b/crates/fuzzy/src/strings.rs index e1f6de37a572b3f6c7f0f676f8ad5dda7ba53498..1a5fb2b02f89147110f29e47584281ad150419b5 100644 --- a/crates/fuzzy/src/strings.rs +++ b/crates/fuzzy/src/strings.rs @@ -61,9 +61,23 @@ impl StringMatch { let mut positions = self.positions.iter().peekable(); iter::from_fn(move || { if let Some(start) = positions.next().copied() { + if start >= self.string.len() { + log::error!( + "Invariant violation: Index {start} out of range in string {:?}", + self.string + ); + return None; + } let mut end = start + self.char_len_at_index(start); while let Some(next_start) = positions.peek() { if end == **next_start { + if end >= self.string.len() { + log::error!( + "Invariant violation: Index {end} out of range in string {:?}", + self.string + ); + return None; + } end += self.char_len_at_index(end); positions.next(); } else {