From edcd573b6ff5bfa8e52918a0fafb67d524516e38 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 19 Jun 2024 12:16:18 +0200 Subject: [PATCH] fix(sdk): Fix a substract with overflow in `LinkedChunk`. --- .../src/event_cache/linked_chunk/mod.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs b/crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs index 2d9c6db78..da4d4c9b2 100644 --- a/crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs +++ b/crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs @@ -1029,7 +1029,7 @@ impl Chunk { match &self.content { ChunkContent::Gap(..) => Position(identifier, 0), - ChunkContent::Items(items) => Position(identifier, items.len() - 1), + ChunkContent::Items(items) => Position(identifier, items.len().saturating_sub(1)), } } @@ -1606,6 +1606,14 @@ mod tests { assert_matches!(iterator.next(), None); } + #[test] + fn test_ritems_empty() { + let linked_chunk = LinkedChunk::<2, char, ()>::new(); + let mut iterator = linked_chunk.ritems(); + + assert_matches!(iterator.next(), None); + } + #[test] fn test_items() { let mut linked_chunk = LinkedChunk::<2, char, ()>::new(); @@ -1623,6 +1631,14 @@ mod tests { assert_matches!(iterator.next(), None); } + #[test] + fn test_items_empty() { + let linked_chunk = LinkedChunk::<2, char, ()>::new(); + let mut iterator = linked_chunk.items(); + + assert_matches!(iterator.next(), None); + } + #[test] fn test_ritems_from() -> Result<(), Error> { let mut linked_chunk = LinkedChunk::<2, char, ()>::new();