Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Fix underflow panic in block map sync when blocks overlap (#51078)

Lukas Wirth8d5689a7faa7
diff --git a/crates/editor/src/display_map/block_map.rs b/crates/editor/src/display_map/block_map.rs
index 2673baae84a..d45165660d9 100644
--- a/crates/editor/src/display_map/block_map.rs
+++ b/crates/editor/src/display_map/block_map.rs
@@ -1091,23 +1091,29 @@ impl BlockMap {
};
let rows_before_block;
- match block_placement {
- BlockPlacement::Above(position) => {
- rows_before_block = position - new_transforms.summary().input_rows;
+ let input_rows = new_transforms.summary().input_rows;
+ match &block_placement {
+ &BlockPlacement::Above(position) => {
+ let Some(delta) = position.checked_sub(input_rows) else {
+ continue;
+ };
+ rows_before_block = delta;
just_processed_folded_buffer = false;
}
- BlockPlacement::Near(position) | BlockPlacement::Below(position) => {
+ &BlockPlacement::Near(position) | &BlockPlacement::Below(position) => {
if just_processed_folded_buffer {
continue;
}
- if position + RowDelta(1) < new_transforms.summary().input_rows {
+ let Some(delta) = (position + RowDelta(1)).checked_sub(input_rows) else {
continue;
- }
- rows_before_block =
- (position + RowDelta(1)) - new_transforms.summary().input_rows;
+ };
+ rows_before_block = delta;
}
- BlockPlacement::Replace(ref range) => {
- rows_before_block = *range.start() - new_transforms.summary().input_rows;
+ BlockPlacement::Replace(range) => {
+ let Some(delta) = range.start().checked_sub(input_rows) else {
+ continue;
+ };
+ rows_before_block = delta;
summary.input_rows = WrapRow(1) + (*range.end() - *range.start());
just_processed_folded_buffer = matches!(block, Block::FoldedBuffer { .. });
}
diff --git a/crates/editor/src/display_map/dimensions.rs b/crates/editor/src/display_map/dimensions.rs
index fd8efa6ca53..0bee934f8f8 100644
--- a/crates/editor/src/display_map/dimensions.rs
+++ b/crates/editor/src/display_map/dimensions.rs
@@ -41,6 +41,10 @@ macro_rules! impl_for_row_types {
pub fn saturating_sub(self, other: $row_delta) -> $row {
$row(self.0.saturating_sub(other.0))
}
+
+ pub fn checked_sub(self, other: $row) -> Option<$row_delta> {
+ self.0.checked_sub(other.0).map($row_delta)
+ }
}
impl ::std::ops::Add for $row {