[llvm] a73ed0c - [Support] Fix SpecificBumpPtrAllocator move assignment leak (#230480)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 11 00:35:26 PDT 2026


Author: Gensoul
Date: 2026-10-11T00:35:21-07:00
New Revision: a73ed0c558c63d094199a362de66fdd6067111fc

URL: https://github.com/llvm/llvm-project/commit/a73ed0c558c63d094199a362de66fdd6067111fc
DIFF: https://github.com/llvm/llvm-project/commit/a73ed0c558c63d094199a362de66fdd6067111fc.diff

LOG: [Support] Fix SpecificBumpPtrAllocator move assignment leak (#230480)

Move assignment releases the destination's storage without destroying
its objects, leaking resources they own.

Call `DestroyAll()` before replacing the destination's storage.

Testing on Linux with GCC 11.4.0, Release with assertions enabled:
- `AllocatorTest.*`: 15 passed. The new regression test fails with the
  original header and passes with the fix.
- Support unit tests via `llvm-lit`: 1842 passed, 13 skipped, no
failures.
- ASan/LSan reproducer: a 512-byte leak with the original header; no
  sanitizer report with the fix.
- Additional ASan/LSan checks for destruction, move construction, empty
and
  populated destinations, multiple slabs, and custom-sized slabs passed.

Assisted-by: Codex

Added: 
    

Modified: 
    llvm/include/llvm/Support/Allocator.h
    llvm/unittests/Support/AllocatorTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/Allocator.h b/llvm/include/llvm/Support/Allocator.h
index 63b18ac077da2..d36d0ff58e490 100644
--- a/llvm/include/llvm/Support/Allocator.h
+++ b/llvm/include/llvm/Support/Allocator.h
@@ -415,6 +415,7 @@ template <typename T> class SpecificBumpPtrAllocator {
   ~SpecificBumpPtrAllocator() { DestroyAll(); }
 
   SpecificBumpPtrAllocator &operator=(SpecificBumpPtrAllocator &&RHS) {
+    DestroyAll();
     Allocator = std::move(RHS.Allocator);
     return *this;
   }

diff  --git a/llvm/unittests/Support/AllocatorTest.cpp b/llvm/unittests/Support/AllocatorTest.cpp
index dbc715b6abb6b..f6682e57fd3d7 100644
--- a/llvm/unittests/Support/AllocatorTest.cpp
+++ b/llvm/unittests/Support/AllocatorTest.cpp
@@ -9,6 +9,7 @@
 #include "llvm/Support/Allocator.h"
 #include "gtest/gtest.h"
 #include <cstdlib>
+#include <utility>
 
 using namespace llvm;
 
@@ -325,4 +326,25 @@ TEST(AllocatorTest, TestOverAlignedSpecific) {
   EXPECT_EQ(4u, NumDtorCalls);
 }
 
+TEST(AllocatorTest, TestSpecificMoveAssignment) {
+  struct S {
+    unsigned &Calls;
+    ~S() { ++Calls; }
+  };
+  unsigned OldDtorCalls = 0;
+  unsigned NewDtorCalls = 0;
+  {
+    SpecificBumpPtrAllocator<S> Alloc;
+    SpecificBumpPtrAllocator<S> Other;
+    new (Alloc.Allocate()) S{OldDtorCalls};
+    new (Other.Allocate()) S{NewDtorCalls};
+
+    Alloc = std::move(Other);
+    EXPECT_EQ(1u, OldDtorCalls);
+    EXPECT_EQ(0u, NewDtorCalls);
+  }
+  EXPECT_EQ(1u, OldDtorCalls);
+  EXPECT_EQ(1u, NewDtorCalls);
+}
+
 }  // anonymous namespace


        


More information about the llvm-commits mailing list