summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
authorSerge Pavlov <sepavloff@gmail.com>2018-02-21 02:02:39 +0000
committerSerge Pavlov <sepavloff@gmail.com>2018-02-21 02:02:39 +0000
commit52525730a163c56f82a43584faa8acb8fd45e1da (patch)
treebd0bbd8442cdba5a7fd40b2555bfa0a7ac1e02ff /clang/lib
parent56492f9177e57b383d54366f93d77595ee80fd78 (diff)
downloadbcm5719-llvm-52525730a163c56f82a43584faa8acb8fd45e1da.tar.gz
bcm5719-llvm-52525730a163c56f82a43584faa8acb8fd45e1da.zip
Clean up use of C allocation functions
If the value returned by `malloc`, `calloc` or `realloc` is not checked for null pointer, this change replaces them for `safe_malloc`, `safe_calloc` or `safe_realloc`, which are defined in the namespace `llvm`. These function report fatal error on out of memory. In the plain C files, assertion statements are added to ensure that memory is successfully allocated. The aim of this change is to get better diagnostics of OOM on Windows. Differential Revision: https://reviews.llvm.org/D43017 llvm-svn: 325661
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/AST/NestedNameSpecifier.cpp2
-rw-r--r--clang/lib/Frontend/CacheTokens.cpp3
-rw-r--r--clang/lib/Frontend/Rewrite/HTMLPrint.cpp7
-rw-r--r--clang/lib/Lex/MacroArgs.cpp3
4 files changed, 8 insertions, 7 deletions
diff --git a/clang/lib/AST/NestedNameSpecifier.cpp b/clang/lib/AST/NestedNameSpecifier.cpp
index 889f8308a93..f46552e337e 100644
--- a/clang/lib/AST/NestedNameSpecifier.cpp
+++ b/clang/lib/AST/NestedNameSpecifier.cpp
@@ -466,7 +466,7 @@ static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
unsigned NewCapacity = std::max(
(unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
(unsigned)(BufferSize + (End - Start)));
- char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
+ char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity));
if (BufferCapacity) {
memcpy(NewBuffer, Buffer, BufferSize);
free(Buffer);
diff --git a/clang/lib/Frontend/CacheTokens.cpp b/clang/lib/Frontend/CacheTokens.cpp
index 72e8f68dc05..851ea25e9b1 100644
--- a/clang/lib/Frontend/CacheTokens.cpp
+++ b/clang/lib/Frontend/CacheTokens.cpp
@@ -662,7 +662,8 @@ std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
// (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
// Note that we use 'calloc', so all the bytes are 0.
- PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
+ PTHIdKey *IIDMap = static_cast<PTHIdKey*>(
+ llvm::safe_calloc(idcount, sizeof(PTHIdKey)));
// Create the hashtable.
llvm::OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
diff --git a/clang/lib/Frontend/Rewrite/HTMLPrint.cpp b/clang/lib/Frontend/Rewrite/HTMLPrint.cpp
index 11e431de0a3..34ee9673cc5 100644
--- a/clang/lib/Frontend/Rewrite/HTMLPrint.cpp
+++ b/clang/lib/Frontend/Rewrite/HTMLPrint.cpp
@@ -86,8 +86,7 @@ void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
// Emit the HTML.
const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
- char *Buffer = (char*)malloc(RewriteBuf.size());
- std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
- Out->write(Buffer, RewriteBuf.size());
- free(Buffer);
+ std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);
+ std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
+ Out->write(Buffer.get(), RewriteBuf.size());
}
diff --git a/clang/lib/Lex/MacroArgs.cpp b/clang/lib/Lex/MacroArgs.cpp
index 5c0f0623c3e..df3f8a4877a 100644
--- a/clang/lib/Lex/MacroArgs.cpp
+++ b/clang/lib/Lex/MacroArgs.cpp
@@ -49,7 +49,8 @@ MacroArgs *MacroArgs::create(const MacroInfo *MI,
if (!ResultEnt) {
// Allocate memory for a MacroArgs object with the lexer tokens at the end,
// and construct the MacroArgs object.
- Result = new (std::malloc(totalSizeToAlloc<Token>(UnexpArgTokens.size())))
+ Result = new (
+ llvm::safe_malloc(totalSizeToAlloc<Token>(UnexpArgTokens.size())))
MacroArgs(UnexpArgTokens.size(), VarargsElided, MI->getNumParams());
} else {
Result = *ResultEnt;
OpenPOWER on IntegriCloud