From 2975ccf2e032dfa03bd2d709f4c0efd21c1b9a1f Mon Sep 17 00:00:00 2001 From: Klaus Kretzschmar Date: Fri, 19 Jan 2018 14:17:53 +0000 Subject: [PATCH] [Support] - Check nullptr after allocation with malloc in MallocAllocator - Differential Revision: http://reviews.llvm.org/D34753 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322944 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/Allocator.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/llvm/Support/Allocator.h b/include/llvm/Support/Allocator.h index a94aa8fb1f2a..7f9c39345b43 100644 --- a/include/llvm/Support/Allocator.h +++ b/include/llvm/Support/Allocator.h @@ -24,6 +24,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Support/ErrorHandling.h" #include #include #include @@ -94,7 +95,11 @@ class MallocAllocator : public AllocatorBase { LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size, size_t /*Alignment*/) { - return malloc(Size); + void* memPtr = malloc(Size); + if (memPtr == nullptr) + report_bad_alloc_error("Allocation in MallocAllocator failed."); + + return memPtr; } // Pull in base class overloads.