Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 31e7990

Browse files
committed
fix Vec leak with 0 capacity
1 parent f262ca1 commit 31e7990

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

library/alloc/src/raw_vec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ impl<T, A: Allocator> RawVec<T, A> {
170170
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
171171
if mem::size_of::<T>() == 0 {
172172
Self::new_in(alloc)
173+
} else if capacity == 0 {
174+
// Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
175+
Self {
176+
ptr: unsafe { Unique::new_unchecked(NonNull::dangling().as_ptr()) },
177+
cap: capacity,
178+
alloc,
179+
}
173180
} else {
174181
// We avoid `unwrap_or_else` here because it bloats the amount of
175182
// LLVM IR generated.

0 commit comments

Comments
 (0)