From 263bc61ec8dfdd25fa0200462de9d2556be899a7 Mon Sep 17 00:00:00 2001 From: Matthias Ahouansou Date: Fri, 6 Jun 2025 17:53:58 +0100 Subject: [PATCH] fix(sync v5): fix underflow when clamping room list indexes & allow empty range both these issues could both previously cause panics when the user is not in many rooms --- src/api/client_server/sync.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index 356c7155..9259d8d9 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -1531,10 +1531,13 @@ pub async fn sync_events_v5_route( let mut new_known_rooms = BTreeSet::new(); for (mut start, mut end) in list.ranges { - start = start.clamp(uint!(0), UInt::from(all_joined_rooms.len() as u32 - 1)); - end = end.clamp(start, UInt::from(all_joined_rooms.len() as u32 - 1)); + start = start.clamp( + uint!(0), + UInt::from(all_joined_rooms.len().saturating_sub(1) as u32), + ); + end = end.clamp(start, UInt::from(all_joined_rooms.len() as u32)); let room_ids = - all_joined_rooms[(u64::from(start) as usize)..=(u64::from(end) as usize)].to_vec(); + all_joined_rooms[(u64::from(start) as usize)..(u64::from(end) as usize)].to_vec(); new_known_rooms.extend(room_ids.iter().cloned()); for room_id in &room_ids { let todo_room =