about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSparkyPotato <noob.sparkypotato@gmail.com>2022-04-06 01:32:26 +0530
committerSparkyPotato <noob.sparkypotato@gmail.com>2022-04-06 01:32:26 +0530
commit31e79901454264b4e3ad22a207bf5df9d6aef9a5 (patch)
tree3e5b7453f3c0dd58015d361fd8e99f67f9b49a66
parentf262ca12aac76152c4b46cefcf8300f0249a5eb2 (diff)
downloadrust-31e79901454264b4e3ad22a207bf5df9d6aef9a5.tar.gz
rust-31e79901454264b4e3ad22a207bf5df9d6aef9a5.zip
fix Vec leak with 0 capacity
-rw-r--r--library/alloc/src/raw_vec.rs7
1 files changed, 7 insertions, 0 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs
index 0ce2beb63d6..5cf190423e3 100644
--- a/library/alloc/src/raw_vec.rs
+++ b/library/alloc/src/raw_vec.rs
@@ -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.