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

HeaderMap::from_iter panics when size is known #604

Open
jongiddy opened this issue Apr 27, 2023 · 0 comments
Open

HeaderMap::from_iter panics when size is known #604

jongiddy opened this issue Apr 27, 2023 · 0 comments

Comments

@jongiddy
Copy link

In this code, HeaderMap::from_iter panics for a set of headers that can fit into a HeaderMap. Most likely the size hint should be capped to 24576 to avoid this panic until too many names are appended.

use http::{HeaderMap, HeaderName, HeaderValue};

fn main() {
    // If we add 24576 names here, then we cannot append extra values.
    let mut headers = (0..24575)
        .map(|i| {
            (
                HeaderName::from_bytes(format!("H{}", i).as_bytes()).unwrap(),
                HeaderValue::from_static("0"),
            )
        })
        .collect::<HeaderMap>();
    // Append more values to existing name
    let mut i = 1;
    while i < 8195 && headers.append(
        HeaderName::from_bytes(format!("H{}", 1).as_bytes()).unwrap(),
        HeaderValue::from_str(&i.to_string()).unwrap(),
    ) {
        i += 1;
    }

    assert_eq!(headers.len(), 32769);

    // Does not panic
    HeaderMap::from_iter(headers.iter().map(|(n, v)| (n.clone(), v.clone())));

    // Panics
    HeaderMap::from_iter(headers.iter().map(|(n, v)| (n.clone(), v.clone())).collect::<Vec<_>>());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant