From 0a93e24ef1280623a34c65fd08e8dfde6f3add61 Mon Sep 17 00:00:00 2001 From: Ariaj Sarkar Date: Mon, 27 Oct 2025 02:15:39 +0530 Subject: [PATCH] fix: update to_raw_capacity to return Result for better error handling --- src/header/map.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/header/map.rs b/src/header/map.rs index e3f3d342..cdc23ae9 100644 --- a/src/header/map.rs +++ b/src/header/map.rs @@ -552,7 +552,8 @@ impl HeaderMap { if capacity == 0 { Ok(Self::default()) } else { - let raw_cap = match to_raw_capacity(capacity).checked_next_power_of_two() { + let raw_cap = to_raw_capacity(capacity)?; + let raw_cap = match raw_cap.checked_next_power_of_two() { Some(c) => c, None => return Err(MaxSizeReached { _priv: () }), }; @@ -750,7 +751,7 @@ impl HeaderMap { .checked_add(additional) .ok_or_else(MaxSizeReached::new)?; - let raw_cap = to_raw_capacity(cap); + let raw_cap = to_raw_capacity(cap)?; if raw_cap > self.indices.len() { let raw_cap = raw_cap @@ -3621,14 +3622,8 @@ fn usable_capacity(cap: usize) -> usize { } #[inline] -fn to_raw_capacity(n: usize) -> usize { - match n.checked_add(n / 3) { - Some(n) => n, - None => panic!( - "requested capacity {} too large: overflow while converting to raw capacity", - n - ), - } +fn to_raw_capacity(n: usize) -> Result { + n.checked_add(n / 3).ok_or_else(MaxSizeReached::new) } #[inline]