summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/FoldingSet.cpp
diff options
context:
space:
mode:
authorMatthias Braun <matze@braunis.de>2017-07-20 01:30:39 +0000
committerMatthias Braun <matze@braunis.de>2017-07-20 01:30:39 +0000
commitc20b3383b78473fdfb958ebfe15227bded79ba78 (patch)
treefce0056e354337b5855270a434d6dee9b777b3cf /llvm/lib/Support/FoldingSet.cpp
parent36a1c17dffa6f2f2bddf9d7974849e9ecee42916 (diff)
downloadbcm5719-llvm-c20b3383b78473fdfb958ebfe15227bded79ba78.tar.gz
bcm5719-llvm-c20b3383b78473fdfb958ebfe15227bded79ba78.zip
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/FoldingSet.cpp')
-rw-r--r--llvm/lib/Support/FoldingSet.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp
index 4496d06a15f..94237954903 100644
--- a/llvm/lib/Support/FoldingSet.cpp
+++ b/llvm/lib/Support/FoldingSet.cpp
@@ -215,6 +215,10 @@ static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {
/// AllocateBuckets - Allocated initialized bucket memory.
static void **AllocateBuckets(unsigned NumBuckets) {
void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*)));
+
+ if (Buckets == nullptr)
+ report_bad_alloc_error("Allocation of Buckets failed.");
+
// Set the very last bucket to be a non-null "pointer".
Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
return Buckets;
@@ -271,10 +275,11 @@ void FoldingSetBase::GrowBucketCount(unsigned NewBucketCount) {
assert(isPowerOf2_32(NewBucketCount) && "Bad bucket count!");
void **OldBuckets = Buckets;
unsigned OldNumBuckets = NumBuckets;
- NumBuckets = NewBucketCount;
// Clear out new buckets.
- Buckets = AllocateBuckets(NumBuckets);
+ Buckets = AllocateBuckets(NewBucketCount);
+ // Set NumBuckets only if allocation of new buckets was succesful
+ NumBuckets = NewBucketCount;
NumNodes = 0;
// Walk the old buckets, rehashing nodes into their new place.
OpenPOWER on IntegriCloud