aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/MemoryBuffer.cpp
diff options
context:
space:
mode:
authorAaron Ballman <aaron@aaronballman.com>2024-05-15 12:33:54 -0400
committerAaron Ballman <aaron@aaronballman.com>2024-05-15 12:36:19 -0400
commit3f954f575156bce8ac81d6b4d94de443786befed (patch)
tree9de7ff10779b52b0ae1736365334a038797e8950 /llvm/lib/Support/MemoryBuffer.cpp
parent29c2475f215110d9e6b3955d5eb2832b3f719c2f (diff)
downloadllvm-3f954f575156bce8ac81d6b4d94de443786befed.zip
llvm-3f954f575156bce8ac81d6b4d94de443786befed.tar.gz
llvm-3f954f575156bce8ac81d6b4d94de443786befed.tar.bz2
Correct mismatched allocation/deallocation calls
This amends dceaa0f4491ebe30c0b0f1bc7fa5ec365b60ced6 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.
Diffstat (limited to 'llvm/lib/Support/MemoryBuffer.cpp')
-rw-r--r--llvm/lib/Support/MemoryBuffer.cpp14
1 files changed, 11 insertions, 3 deletions
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index 50308bd..fb7e804 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/lib/Support/MemoryBuffer.cpp
@@ -79,8 +79,16 @@ void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
SmallString<256> NameBuf;
StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
- char *Mem = static_cast<char *>(operator new(N + sizeof(size_t) +
- NameRef.size() + 1));
+ // We use malloc() and manually handle it returning null instead of calling
+ // operator new because we need all uses of NamedBufferAlloc to be
+ // deallocated with a call to free() due to needing to use malloc() in
+ // WritableMemoryBuffer::getNewUninitMemBuffer() to work around the out-of-
+ // memory handler installed by default in LLVM. See operator delete() member
+ // functions within this file for the paired call to free().
+ char *Mem =
+ static_cast<char *>(std::malloc(N + sizeof(size_t) + NameRef.size() + 1));
+ if (!Mem)
+ llvm::report_bad_alloc_error("Allocation failed");
*reinterpret_cast<size_t *>(Mem + N) = NameRef.size();
CopyStringRef(Mem + N + sizeof(size_t), NameRef);
return Mem;
@@ -225,7 +233,7 @@ public:
/// Disable sized deallocation for MemoryBufferMMapFile, because it has
/// tail-allocated data.
- void operator delete(void *p) { ::operator delete(p); }
+ void operator delete(void *p) { std::free(p); }
StringRef getBufferIdentifier() const override {
// The name is stored after the class itself.