diff options
author | Matthias Braun <matze@braunis.de> | 2017-07-20 01:30:39 +0000 |
---|---|---|
committer | Matthias Braun <matze@braunis.de> | 2017-07-20 01:30:39 +0000 |
commit | c20b3383b78473fdfb958ebfe15227bded79ba78 (patch) | |
tree | fce0056e354337b5855270a434d6dee9b777b3cf /llvm/lib/Support/SmallPtrSet.cpp | |
parent | 36a1c17dffa6f2f2bddf9d7974849e9ecee42916 (diff) | |
download | llvm-c20b3383b78473fdfb958ebfe15227bded79ba78.zip llvm-c20b3383b78473fdfb958ebfe15227bded79ba78.tar.gz llvm-c20b3383b78473fdfb958ebfe15227bded79ba78.tar.bz2 |
Support, IR, ADT: Check nullptr after allocation with malloc/realloc or calloc
As a follow up of the bad alloc handler patch, this patch introduces nullptr checks on pointers returned from the
malloc/realloc/calloc functions. In addition some memory size assignments are moved behind the allocation
of the corresponding memory to fulfill exception safe memory management (RAII).
patch by Klaus Kretzschmar
Differential Revision: https://reviews.llvm.org/D35414
llvm-svn: 308576
Diffstat (limited to 'llvm/lib/Support/SmallPtrSet.cpp')
-rw-r--r-- | llvm/lib/Support/SmallPtrSet.cpp | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp index aa12e85..47e960e 100644 --- a/llvm/lib/Support/SmallPtrSet.cpp +++ b/llvm/lib/Support/SmallPtrSet.cpp @@ -15,6 +15,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/DenseMapInfo.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Support/ErrorHandling.h" #include <algorithm> #include <cassert> #include <cstdlib> @@ -32,7 +33,9 @@ void SmallPtrSetImplBase::shrink_and_clear() { // Install the new array. Clear all the buckets to empty. CurArray = (const void**)malloc(sizeof(void*) * CurArraySize); - assert(CurArray && "Failed to allocate memory?"); + if (CurArray == nullptr) + report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed."); + memset(CurArray, -1, CurArraySize*sizeof(void*)); } @@ -96,8 +99,12 @@ void SmallPtrSetImplBase::Grow(unsigned NewSize) { bool WasSmall = isSmall(); // Install the new array. Clear all the buckets to empty. - CurArray = (const void**)malloc(sizeof(void*) * NewSize); - assert(CurArray && "Failed to allocate memory?"); + const void **NewBuckets = (const void**) malloc(sizeof(void*) * NewSize); + if (NewBuckets == nullptr) + report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed."); + + // Reset member only if memory was allocated successfully + CurArray = NewBuckets; CurArraySize = NewSize; memset(CurArray, -1, NewSize*sizeof(void*)); @@ -125,7 +132,8 @@ SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage, // Otherwise, allocate new heap space (unless we were the same size) } else { CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize); - assert(CurArray && "Failed to allocate memory?"); + if (CurArray == nullptr) + report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed."); } // Copy over the that array. @@ -162,7 +170,8 @@ void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) { free(CurArray); CurArray = T; } - assert(CurArray && "Failed to allocate memory?"); + if (CurArray == nullptr) + report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed."); } CopyHelper(RHS); |