[PATCH] D42240: [Support] - Check nullptr after allocation with malloc in MallocAllocator

Klaus Kretzschmar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 18 06:44:23 PST 2018


kkretzsch created this revision.
kkretzsch added reviewers: hans, MatzeB.

As a follow up of our nullptr check patches,

https://reviews.llvm.org/D34753
https://reviews.llvm.org/D35414

this patch introduces a nullptr checks on the llvm MallocAllocator class itself.


Repository:
  rL LLVM

https://reviews.llvm.org/D42240

Files:
  include/llvm/Support/Allocator.h


Index: include/llvm/Support/Allocator.h
===================================================================
--- include/llvm/Support/Allocator.h
+++ 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 <algorithm>
 #include <cassert>
 #include <cstddef>
@@ -94,7 +95,11 @@
 
   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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42240.130401.patch
Type: text/x-patch
Size: 801 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180118/c77ceecb/attachment.bin>


More information about the llvm-commits mailing list