From 81ceb611cf96abe91d91693e813cd5ee36cdae02 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 25 Nov 2019 15:54:04 -0800 Subject: [PATCH] fix capacity overflows in HeaderMap::reserve --- src/header/map.rs | 2 ++ tests/header_map.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/header/map.rs b/src/header/map.rs index 38447fa5..db0fda84 100644 --- a/src/header/map.rs +++ b/src/header/map.rs @@ -638,6 +638,8 @@ impl HeaderMap { if cap > self.indices.len() { let cap = cap.next_power_of_two(); + assert!(cap < MAX_SIZE, "header map reserve over max capacity"); + assert!(cap != 0, "header map reserve overflowed"); if self.entries.len() == 0 { self.mask = cap - 1; diff --git a/tests/header_map.rs b/tests/header_map.rs index 01ae369a..f84c0198 100644 --- a/tests/header_map.rs +++ b/tests/header_map.rs @@ -37,6 +37,22 @@ fn smoke() { } } +#[test] +#[should_panic] +fn reserve_over_capacity() { + // See https://github.com/hyperium/http/issues/352 + let mut headers = HeaderMap::::with_capacity(32); + headers.reserve(50_000); // over MAX_SIZE +} + +#[test] +#[should_panic] +fn reserve_overflow() { + // See https://github.com/hyperium/http/issues/352 + let mut headers = HeaderMap::::with_capacity(0); + headers.reserve(std::usize::MAX); // next_power_of_two overflows +} + #[test] fn drain() { let mut headers = HeaderMap::new();