quinn-proto: make cids_exhausted overflow-safe on 32-bit

This commit is contained in:
Kevin R
2026-02-13 21:09:00 +01:00
committed by Dirkjan Ochtman
parent e841ce456d
commit e96c279437
+12 -5
View File
@@ -913,11 +913,18 @@ impl Endpoint {
/// We leave some space unused so that `new_cid` can be relied upon to finish quickly. We don't
/// bother to check when CID longer than 4 bytes are used because 2^40 connections is a lot.
fn cids_exhausted(&self) -> bool {
self.local_cid_generator.cid_len() <= 4
&& self.local_cid_generator.cid_len() != 0
&& (2usize.pow(self.local_cid_generator.cid_len() as u32 * 8)
- self.index.connection_ids.len())
< 2usize.pow(self.local_cid_generator.cid_len() as u32 * 8 - 2)
let cid_len = self.local_cid_generator.cid_len();
if cid_len == 0 || cid_len > 4 {
return false;
}
// Keep this architecture-independent: on 32-bit targets, 2usize.pow(32) overflows.
let bits = (cid_len * 8) as u32;
let space = 1u64 << bits;
let reserve = 1u64 << (bits - 2);
let len = self.index.connection_ids.len() as u64;
len > (space - reserve)
}
}