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

Arithmetic overflow found on with_capacity() #627

Open
HeeillWang opened this issue Sep 19, 2023 · 4 comments
Open

Arithmetic overflow found on with_capacity() #627

HeeillWang opened this issue Sep 19, 2023 · 4 comments

Comments

@HeeillWang
Copy link
Contributor

I executed fuzz testing on http-0.2.9, and found some arithmetic overflow.
Please note that overflow condition is different with #626 .

    pub fn with_capacity(capacity: usize) -> HeaderMap<T> {
        if capacity == 0 {
            HeaderMap {
                mask: 0,
                indices: Box::new([]), // as a ZST, this doesn't actually allocate anything
                entries: Vec::new(),
                extra_values: Vec::new(),
                danger: Danger::Green,
            }
        } else {
            let raw_cap = to_raw_capacity(capacity).next_power_of_two();   // overflow!
            assert!(raw_cap <= MAX_SIZE, "requested capacity too large");
            debug_assert!(raw_cap > 0);

            HeaderMap {
                mask: (raw_cap - 1) as Size,
                indices: vec![Pos::none(); raw_cap].into_boxed_slice(),
                entries: Vec::with_capacity(raw_cap),
                extra_values: Vec::new(),
                danger: Danger::Green,
            }
        }
    }

reproduce with :

HeaderMap::<u32>::with_capacity(12538021362599493900);  // put some big number here

If you input TOO big number on with_capacity(), #626 occurs before reaching to next_power_of_two().

@hawkw
Copy link
Contributor

hawkw commented Sep 19, 2023

It seems like there should be an assertion prior to the next_power_of_two call, in addition to the one after it?

@HeeillWang
Copy link
Contributor Author

That would resolve #626 as well.
Plus, consider to use checked_next_power_of_two

@seanmonstar
Copy link
Member

Thanks for detecting those! Would you like to submit a PR that uses an assertion or checked math?

@HeeillWang
Copy link
Contributor Author

submitted on #628

seanmonstar pushed a commit that referenced this issue Nov 10, 2023
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

3 participants