From c567928c430262295b3a587ecc3aa00f657f4704 Mon Sep 17 00:00:00 2001 From: Ariaj Sarkar Date: Mon, 24 Nov 2025 00:15:56 +0530 Subject: [PATCH] feat: Add `HeaderMap` implementation with `try_insert` and `MaxSizeReached` error for capacity overflow handling. --- src/header/map.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/header/map.rs b/src/header/map.rs index e670f484..387f6a6c 100644 --- a/src/header/map.rs +++ b/src/header/map.rs @@ -743,13 +743,17 @@ impl HeaderMap { /// # map.try_insert(HOST, "bar".parse().unwrap()).unwrap(); /// ``` pub fn try_reserve(&mut self, additional: usize) -> Result<(), MaxSizeReached> { - // TODO: This can't overflow if done properly... since the max # of - // elements is u16::MAX. - let cap = self - .entries - .len() - .checked_add(additional) - .ok_or_else(MaxSizeReached::new)?; + // Early bounds check: Since self.entries.len() <= MAX_SIZE (invariant), + // and MAX_SIZE fits in u16, we can avoid checked_add by validating + // that additional won't cause the total to exceed MAX_SIZE. + let current_len = self.entries.len(); + if additional > MAX_SIZE.saturating_sub(current_len) { + return Err(MaxSizeReached::new()); + } + + // Safe: We've verified that current_len + additional <= MAX_SIZE, + // which is well within usize range, so no overflow is possible. + let cap = current_len + additional; let raw_cap = to_raw_capacity(cap)?;