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
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user