Skip to content

Commit b452749

Browse files
authored
Rollup merge of rust-lang#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.
2 parents acdba55 + 83f659b commit b452749

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

library/alloc/src/raw_vec.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ impl<T, A: Allocator> RawVec<T, A> {
168168

169169
#[cfg(not(no_global_oom_handling))]
170170
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
171-
if mem::size_of::<T>() == 0 {
171+
// Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
172+
if mem::size_of::<T>() == 0 || capacity == 0 {
172173
Self::new_in(alloc)
173174
} else {
174175
// We avoid `unwrap_or_else` here because it bloats the amount of

0 commit comments

Comments
 (0)