Skip to content

Commit e96c279

Browse files
KevinRoebertdjc
authored andcommitted
quinn-proto: make cids_exhausted overflow-safe on 32-bit
1 parent e841ce4 commit e96c279

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

quinn-proto/src/endpoint.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -913,11 +913,18 @@ impl Endpoint {
913913
/// We leave some space unused so that `new_cid` can be relied upon to finish quickly. We don't
914914
/// bother to check when CID longer than 4 bytes are used because 2^40 connections is a lot.
915915
fn cids_exhausted(&self) -> bool {
916-
self.local_cid_generator.cid_len() <= 4
917-
&& self.local_cid_generator.cid_len() != 0
918-
&& (2usize.pow(self.local_cid_generator.cid_len() as u32 * 8)
919-
- self.index.connection_ids.len())
920-
< 2usize.pow(self.local_cid_generator.cid_len() as u32 * 8 - 2)
916+
let cid_len = self.local_cid_generator.cid_len();
917+
if cid_len == 0 || cid_len > 4 {
918+
return false;
919+
}
920+
921+
// Keep this architecture-independent: on 32-bit targets, 2usize.pow(32) overflows.
922+
let bits = (cid_len * 8) as u32;
923+
let space = 1u64 << bits;
924+
let reserve = 1u64 << (bits - 2);
925+
let len = self.index.connection_ids.len() as u64;
926+
927+
len > (space - reserve)
921928
}
922929
}
923930

0 commit comments

Comments
 (0)