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/SmallVector.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/SmallVector.cpp')
-rw-r--r-- | llvm/lib/Support/SmallVector.cpp | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/llvm/lib/Support/SmallVector.cpp b/llvm/lib/Support/SmallVector.cpp index b931505..74313151 100644 --- a/llvm/lib/Support/SmallVector.cpp +++ b/llvm/lib/Support/SmallVector.cpp @@ -26,14 +26,17 @@ void SmallVectorBase::grow_pod(void *FirstEl, size_t MinSizeInBytes, void *NewElts; if (BeginX == FirstEl) { NewElts = malloc(NewCapacityInBytes); + if (NewElts == nullptr) + report_bad_alloc_error("Allocation of SmallVector element failed."); // Copy the elements over. No need to run dtors on PODs. memcpy(NewElts, this->BeginX, CurSizeBytes); } else { // If this wasn't grown from the inline copy, grow the allocated space. NewElts = realloc(this->BeginX, NewCapacityInBytes); + if (NewElts == nullptr) + report_bad_alloc_error("Reallocation of SmallVector element failed."); } - assert(NewElts && "Out of memory"); this->EndX = (char*)NewElts+CurSizeBytes; this->BeginX = NewElts; |