http icon indicating copy to clipboard operation
http copied to clipboard

Inconsistent capacity handling in `HeaderMap::with_capacity`

Open Qwaz opened this issue 6 years ago • 3 comments

https://github.com/hyperium/http/blob/9c05e391e00474abaa8c14a86bcb0fc5eff1120e/src/header/map.rs#L473

https://github.com/hyperium/http/blob/9c05e391e00474abaa8c14a86bcb0fc5eff1120e/src/header/map.rs#L635

https://github.com/hyperium/http/blob/9c05e391e00474abaa8c14a86bcb0fc5eff1120e/src/header/map.rs#L1592

HeaderMap::with_capacity reserves the entry vector with Vec::with_capacity(raw_cap), while other functions reserves the same entry with Vec::with_capacity(usable_capacity(raw_cap)).

Qwaz avatar Nov 16 '19 16:11 Qwaz

@Qwaz could you provide some additional details on why this is incorrect? Thanks!

hawkw avatar Nov 18 '19 22:11 hawkw

@hawkw This is not an incorrect code in terms of correctness, but we can improve the consistency of the code and save a bit of memory by changing HeaderMap::with_capacity to use Vec::with_capacity(usable_capacity(raw_cap)) instead of Vec::with_capacity(raw_cap).

Qwaz avatar Nov 19 '19 02:11 Qwaz

@Qwaz ah, okay, I misunderstood — I thought that since this was linked in the "Audit http" issue on safety-dance, this was part of the security audit. Thanks for clearing that up.

hawkw avatar Nov 19 '19 02:11 hawkw