From bc81ada227061ebadf9d5d3ea32eb5a090dfe26f Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 24 Feb 2026 22:16:50 -0700 Subject: [PATCH] Fix panic in can_resolve when passed an invalid excerpt ID (#50052) Fixes ZED-59F This is follow-up work from #49994; which assumed that can_resolve would return false for an invalid excerpt id. Before you mark this PR as ready for review, make sure that you have: - [ ] Added a solid test coverage and/or screenshots from doing manual testing - [ ] Done a self-review taking into account security and performance aspects - [ ] Aligned any UI changes with the [UI checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) Release Notes: - N/A --- crates/multi_buffer/src/multi_buffer.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rs index 34d32f481947657327cbec99e0a3aedc59aeabe7..6a90d9c410859324d31ebbd59c909e31127ecc6a 100644 --- a/crates/multi_buffer/src/multi_buffer.rs +++ b/crates/multi_buffer/src/multi_buffer.rs @@ -6938,18 +6938,23 @@ impl MultiBufferSnapshot { } fn excerpt_locator_for_id(&self, id: ExcerptId) -> &Locator { + self.try_excerpt_locator_for_id(id) + .unwrap_or_else(|| panic!("invalid excerpt id {id:?}")) + } + + fn try_excerpt_locator_for_id(&self, id: ExcerptId) -> Option<&Locator> { if id == ExcerptId::min() { - Locator::min_ref() + Some(Locator::min_ref()) } else if id == ExcerptId::max() { - Locator::max_ref() + Some(Locator::max_ref()) } else { let (_, _, item) = self.excerpt_ids.find::((), &id, Bias::Left); if let Some(entry) = item && entry.id == id { - return &entry.locator; + return Some(&entry.locator); } - panic!("invalid excerpt id {id:?}") + None } } @@ -7034,7 +7039,7 @@ impl MultiBufferSnapshot { /// afterwards. fn excerpt(&self, excerpt_id: ExcerptId) -> Option<&Excerpt> { let excerpt_id = self.latest_excerpt_id(excerpt_id); - let locator = self.excerpt_locator_for_id(excerpt_id); + let locator = self.try_excerpt_locator_for_id(excerpt_id)?; let (_, _, item) = self.excerpts .find::, _>((), &Some(locator), Bias::Left);