Skip to content

Commit

Permalink
fix Vec leak with 0 capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkyPotato committed Apr 5, 2022
1 parent f262ca1 commit 31e7990
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ impl<T, A: Allocator> RawVec<T, A> {
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
if mem::size_of::<T>() == 0 {
Self::new_in(alloc)
} else if capacity == 0 {
// Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
Self {
ptr: unsafe { Unique::new_unchecked(NonNull::dangling().as_ptr()) },
cap: capacity,
alloc,
}
} else {
// We avoid `unwrap_or_else` here because it bloats the amount of
// LLVM IR generated.
Expand Down

0 comments on commit 31e7990

Please sign in to comment.