From ed3dbc85861ab61fee56077c7ba94306b0a96dc4 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 22 Aug 2022 23:11:43 +0300 Subject: [PATCH] listing/conversations: fix crashes when listing is empty --- src/components/mail/listing/conversations.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/components/mail/listing/conversations.rs b/src/components/mail/listing/conversations.rs index c3320ffe..f73c67d3 100644 --- a/src/components/mail/listing/conversations.rs +++ b/src/components/mail/listing/conversations.rs @@ -403,14 +403,14 @@ impl ListingTrait for ConversationsListing { if self.new_cursor_pos.2 + amount + 1 < self.length { self.new_cursor_pos.2 += amount; } else { - self.new_cursor_pos.2 = self.length - 1; + self.new_cursor_pos.2 = self.length.saturating_sub(1); } } PageMovement::PageDown(multiplier) => { if self.new_cursor_pos.2 + rows * multiplier + 1 < self.length { self.new_cursor_pos.2 += rows * multiplier; } else if self.new_cursor_pos.2 + rows * multiplier > self.length { - self.new_cursor_pos.2 = self.length - 1; + self.new_cursor_pos.2 = self.length.saturating_sub(1); } else { self.new_cursor_pos.2 = (self.length.saturating_sub(1) / rows) * rows; } @@ -451,7 +451,7 @@ impl ListingTrait for ConversationsListing { self.cursor_pos = self.new_cursor_pos; } if self.new_cursor_pos.2 >= self.length { - self.new_cursor_pos.2 = self.length - 1; + self.new_cursor_pos.2 = self.length.saturating_sub(1); self.cursor_pos.2 = self.new_cursor_pos.2; } @@ -516,7 +516,7 @@ impl ListingTrait for ConversationsListing { if self.all_threads.contains(&thread) { self.filtered_selection.push(thread); self.filtered_order - .insert(thread, self.filtered_selection.len() - 1); + .insert(thread, self.filtered_selection.len().saturating_sub(1)); } } if !self.filtered_selection.is_empty() { @@ -525,8 +525,10 @@ impl ListingTrait for ConversationsListing { self.sort, &context.accounts[&self.cursor_pos.0].collection.envelopes, ); - self.new_cursor_pos.2 = - std::cmp::min(self.filtered_selection.len() - 1, self.cursor_pos.2); + self.new_cursor_pos.2 = std::cmp::min( + self.filtered_selection.len().saturating_sub(1), + self.cursor_pos.2, + ); } self.redraw_threads_list( context,