mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 01:04:03 +00:00
Rollup merge of #95699 - SparkyPotato:master, r=dtolnay
fix: Vec leak when capacity is 0 When `RawVec::with_capacity_in` is called with capacity 0, an allocation of size 0 is allocated. However, `<RawVec as Drop>::drop` doesn't deallocate, since it only checks if capacity was 0. Fixed by not allocating when capacity is 0.
This commit is contained in:
commit
b4527492b1
@ -168,7 +168,8 @@ impl<T, A: Allocator> RawVec<T, A> {
|
||||
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
|
||||
if mem::size_of::<T>() == 0 {
|
||||
// Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
|
||||
if mem::size_of::<T>() == 0 || capacity == 0 {
|
||||
Self::new_in(alloc)
|
||||
} else {
|
||||
// We avoid `unwrap_or_else` here because it bloats the amount of
|
||||
|
Loading…
Reference in New Issue
Block a user