Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix capacity overflows in HeaderMap::reserve #360

Merged
merged 1 commit into from Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/header/map.rs
Expand Up @@ -638,6 +638,8 @@ impl<T> HeaderMap<T> {

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");
seanmonstar marked this conversation as resolved.
Show resolved Hide resolved

if self.entries.len() == 0 {
self.mask = cap - 1;
Expand Down
16 changes: 16 additions & 0 deletions tests/header_map.rs
Expand Up @@ -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::<u32>::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::<u32>::with_capacity(0);
headers.reserve(std::usize::MAX); // next_power_of_two overflows
}

#[test]
fn drain() {
let mut headers = HeaderMap::new();
Expand Down