Skip to content

Commit 3f954f5

Browse files
committed
Correct mismatched allocation/deallocation calls
This amends dceaa0f because ASAN caught an issue where the allocation and deallocation were not properly paired: https://lab.llvm.org/buildbot/#/builders/239/builds/7001 Use malloc and free throughout this file to ensure that all kinds of memory buffers use the proper pairing.
1 parent 29c2475 commit 3f954f5

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

llvm/lib/Support/MemoryBuffer.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,16 @@ void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
7979
SmallString<256> NameBuf;
8080
StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
8181

82-
char *Mem = static_cast<char *>(operator new(N + sizeof(size_t) +
83-
NameRef.size() + 1));
82+
// We use malloc() and manually handle it returning null instead of calling
83+
// operator new because we need all uses of NamedBufferAlloc to be
84+
// deallocated with a call to free() due to needing to use malloc() in
85+
// WritableMemoryBuffer::getNewUninitMemBuffer() to work around the out-of-
86+
// memory handler installed by default in LLVM. See operator delete() member
87+
// functions within this file for the paired call to free().
88+
char *Mem =
89+
static_cast<char *>(std::malloc(N + sizeof(size_t) + NameRef.size() + 1));
90+
if (!Mem)
91+
llvm::report_bad_alloc_error("Allocation failed");
8492
*reinterpret_cast<size_t *>(Mem + N) = NameRef.size();
8593
CopyStringRef(Mem + N + sizeof(size_t), NameRef);
8694
return Mem;
@@ -225,7 +233,7 @@ class MemoryBufferMMapFile : public MB {
225233

226234
/// Disable sized deallocation for MemoryBufferMMapFile, because it has
227235
/// tail-allocated data.
228-
void operator delete(void *p) { ::operator delete(p); }
236+
void operator delete(void *p) { std::free(p); }
229237

230238
StringRef getBufferIdentifier() const override {
231239
// The name is stored after the class itself.

0 commit comments

Comments
 (0)