Skip to content

Commit

Permalink
Rollup merge of rust-lang#95699 - SparkyPotato:master, r=dtolnay
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dylan-DPC committed Apr 6, 2022
2 parents acdba55 + 83f659b commit b452749
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b452749

Please sign in to comment.