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:
Dylan DPC 2022-04-06 03:39:09 +02:00 committed by GitHub
commit b4527492b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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