[PATCH] D43017: Clean up use of C allocation functions

2018-02-20 Thread Serge Pavlov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL325661: Clean up use of C allocation functions (authored by 
sepavloff, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D43017?vs=133201&id=135185#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D43017

Files:
  cfe/trunk/include/clang/Sema/ParsedTemplate.h
  cfe/trunk/lib/AST/NestedNameSpecifier.cpp
  cfe/trunk/lib/Frontend/CacheTokens.cpp
  cfe/trunk/lib/Frontend/Rewrite/HTMLPrint.cpp
  cfe/trunk/lib/Lex/MacroArgs.cpp
  cfe/trunk/tools/c-index-test/c-index-test.c
  cfe/trunk/tools/libclang/BuildSystem.cpp
  cfe/trunk/tools/libclang/CIndex.cpp
  cfe/trunk/tools/libclang/CXString.cpp

Index: cfe/trunk/include/clang/Sema/ParsedTemplate.h
===
--- cfe/trunk/include/clang/Sema/ParsedTemplate.h
+++ cfe/trunk/include/clang/Sema/ParsedTemplate.h
@@ -199,8 +199,7 @@
SourceLocation LAngleLoc, SourceLocation RAngleLoc,
ArrayRef TemplateArgs,
SmallVectorImpl &CleanupList) {
-
-  TemplateIdAnnotation *TemplateId = new (std::malloc(
+  TemplateIdAnnotation *TemplateId = new (llvm::safe_malloc(
   totalSizeToAlloc(TemplateArgs.size(
   TemplateIdAnnotation(SS, TemplateKWLoc, TemplateNameLoc, Name,
OperatorKind, OpaqueTemplateName, TemplateKind,
Index: cfe/trunk/lib/Frontend/Rewrite/HTMLPrint.cpp
===
--- cfe/trunk/lib/Frontend/Rewrite/HTMLPrint.cpp
+++ cfe/trunk/lib/Frontend/Rewrite/HTMLPrint.cpp
@@ -86,8 +86,7 @@
 
   // 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 Buffer(new char[RewriteBuf.size()]);
+  std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
+  Out->write(Buffer.get(), RewriteBuf.size());
 }
Index: cfe/trunk/lib/Frontend/CacheTokens.cpp
===
--- cfe/trunk/lib/Frontend/CacheTokens.cpp
+++ cfe/trunk/lib/Frontend/CacheTokens.cpp
@@ -662,7 +662,8 @@
   //  (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(
+  llvm::safe_calloc(idcount, sizeof(PTHIdKey)));
 
   // Create the hashtable.
   llvm::OnDiskChainedHashTableGenerator IIOffMap;
Index: cfe/trunk/lib/Lex/MacroArgs.cpp
===
--- cfe/trunk/lib/Lex/MacroArgs.cpp
+++ cfe/trunk/lib/Lex/MacroArgs.cpp
@@ -49,7 +49,8 @@
   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(UnexpArgTokens.size(
+Result = new (
+llvm::safe_malloc(totalSizeToAlloc(UnexpArgTokens.size(
 MacroArgs(UnexpArgTokens.size(), VarargsElided, MI->getNumParams());
   } else {
 Result = *ResultEnt;
Index: cfe/trunk/lib/AST/NestedNameSpecifier.cpp
===
--- cfe/trunk/lib/AST/NestedNameSpecifier.cpp
+++ cfe/trunk/lib/AST/NestedNameSpecifier.cpp
@@ -466,7 +466,7 @@
 unsigned NewCapacity = std::max(
 (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
 (unsigned)(BufferSize + (End - Start)));
-char *NewBuffer = static_cast(malloc(NewCapacity));
+char *NewBuffer = static_cast(llvm::safe_malloc(NewCapacity));
 if (BufferCapacity) {
   memcpy(NewBuffer, Buffer, BufferSize);
   free(Buffer);
Index: cfe/trunk/tools/libclang/BuildSystem.cpp
===
--- cfe/trunk/tools/libclang/BuildSystem.cpp
+++ cfe/trunk/tools/libclang/BuildSystem.cpp
@@ -17,6 +17,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/CBindingWrapping.h"
 #include "llvm/Support/Chrono.h"
+#include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -78,7 +79,7 @@
   unwrap(VFO)->write(OS);
 
   StringRef Data = OS.str();
-  *out_buffer_ptr = (char*)malloc(Data.size());
+  *out_buffer_ptr = static_cast(llvm::safe_malloc(Data.size()));
   *out_buffer_size = Data.size();
   memcpy(*out_buffer_ptr, Data.data(), Data.size());
   return CXError_Success;
@@ -140,7 +141,7 @@
   OS << "}\n";
 
   StringRef Data = OS.str();
-  *out_buffer_ptr = (char*)malloc(Data.size());
+  *out_buffer_ptr = static_cast(llvm::safe_malloc(Data.size()));
   *out_buffer_size = Data.size();
   memcpy(*out_buffer_ptr, Data.data(), Data.size());
   return CXError

[PATCH] D43017: Clean up use of C allocation functions

2018-02-07 Thread Reid Kleckner via Phabricator via cfe-commits
rnk accepted this revision.
rnk added a comment.
This revision is now accepted and ready to land.

lgtm


Repository:
  rC Clang

https://reviews.llvm.org/D43017



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D43017: Clean up use of C allocation functions

2018-02-07 Thread Serge Pavlov via Phabricator via cfe-commits
sepavloff created this revision.
sepavloff added reviewers: akyrtzi, rnk.

This change cleans up uses of malloc/calloc/realloc. In the case where
the return value is not checked agains null pointer, the call to
'std::malloc' is replaced by 'llvm::malloc', which reports fatal error
on allocation failure. In 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.


Repository:
  rC Clang

https://reviews.llvm.org/D43017

Files:
  include/clang/Sema/ParsedTemplate.h
  lib/AST/NestedNameSpecifier.cpp
  lib/Frontend/CacheTokens.cpp
  lib/Frontend/Rewrite/HTMLPrint.cpp
  lib/Lex/MacroArgs.cpp
  tools/c-index-test/c-index-test.c
  tools/libclang/BuildSystem.cpp
  tools/libclang/CIndex.cpp
  tools/libclang/CXString.cpp

Index: tools/libclang/CXString.cpp
===
--- tools/libclang/CXString.cpp
+++ tools/libclang/CXString.cpp
@@ -96,7 +96,7 @@
 
 CXString createDup(StringRef String) {
   CXString Result;
-  char *Spelling = static_cast(malloc(String.size() + 1));
+  char *Spelling = static_cast(llvm::malloc(String.size() + 1));
   memmove(Spelling, String.data(), String.size());
   Spelling[String.size()] = 0;
   Result.data = Spelling;
Index: tools/libclang/CIndex.cpp
===
--- tools/libclang/CIndex.cpp
+++ tools/libclang/CIndex.cpp
@@ -,7 +,8 @@
   if (CXTokens.empty())
 return;
 
-  *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
+  *Tokens = static_cast(
+  llvm::malloc(sizeof(CXToken) * CXTokens.size()));
   memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
   *NumTokens = CXTokens.size();
 }
Index: tools/libclang/BuildSystem.cpp
===
--- tools/libclang/BuildSystem.cpp
+++ tools/libclang/BuildSystem.cpp
@@ -17,6 +17,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/CBindingWrapping.h"
 #include "llvm/Support/Chrono.h"
+#include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -78,7 +79,7 @@
   unwrap(VFO)->write(OS);
 
   StringRef Data = OS.str();
-  *out_buffer_ptr = (char*)malloc(Data.size());
+  *out_buffer_ptr = static_cast(llvm::malloc(Data.size()));
   *out_buffer_size = Data.size();
   memcpy(*out_buffer_ptr, Data.data(), Data.size());
   return CXError_Success;
@@ -140,7 +141,7 @@
   OS << "}\n";
 
   StringRef Data = OS.str();
-  *out_buffer_ptr = (char*)malloc(Data.size());
+  *out_buffer_ptr = static_cast(llvm::malloc(Data.size()));
   *out_buffer_size = Data.size();
   memcpy(*out_buffer_ptr, Data.data(), Data.size());
   return CXError_Success;
Index: tools/c-index-test/c-index-test.c
===
--- tools/c-index-test/c-index-test.c
+++ tools/c-index-test/c-index-test.c
@@ -232,6 +232,7 @@
   *unsaved_files
 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
  *num_unsaved_files);
+  assert(unsaved_files);
   for (i = 0; i != *num_unsaved_files; ++i) {
 struct CXUnsavedFile *unsaved = *unsaved_files + i;
 const char *arg_string = argv[arg_indices[i]] + prefix_len;
@@ -267,6 +268,7 @@
 
 /* Read the contents of the file we're remapping to. */
 contents = (char *)malloc(unsaved->Length + 1);
+assert(contents);
 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
   fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
   (feof(to_file) ? "EOF" : "error"), sep + 1);
@@ -286,6 +288,7 @@
 /* Copy the file name that we're remapping from. */
 filename_len = sep - arg_string;
 filename = (char *)malloc(filename_len + 1);
+assert(filename);
 memcpy(filename, arg_string, filename_len);
 filename[filename_len] = 0;
 unsaved->Filename = filename;
@@ -340,6 +343,7 @@
 = (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx,
   sizeof(struct CXUnsavedFile) *
 *num_unsaved_files);
+  assert(unsaved_files);
   memcpy(*unsaved_files + num_unsaved_files_no_try_idx,
  unsaved_files_try_idx, sizeof(struct CXUnsavedFile) *
 num_unsaved_files_try_idx);
@@ -2169,6 +2173,7 @@
 
   /* Copy the file name. */
   *filename = (char*)malloc(last_colon - input + 1);
+  assert(*filename);
   memcpy(*filename, input, last_colon - input);
   (*filename)[last_colon - input] = 0;
   return 0;
@@ -2578,6 +2583,7 @@
   assert(NumLocations > 0 && "Unable to count locations?");
   Locations = (CursorSourceLocation *)malloc(
   NumLocations * sizeof(CursorSourceLocation));
+  assert(Locations);
   for (Loc = 0; Loc < NumLocations; ++Loc) {
 const char *input = argv[Loc + 1] + strlen(loc