mirror of
https://github.com/n0-computer/noq.git
synced 2026-09-16 16:25:08 +00:00
quinn-proto: make cids_exhausted overflow-safe on 32-bit
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user