[llvm-branch-commits] [cfe-branch] r244223 - Merging r243945-243950.

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Aug 6 08:53:49 PDT 2015


Author: hans
Date: Thu Aug  6 10:53:49 2015
New Revision: 244223

URL: http://llvm.org/viewvc/llvm-project?rev=244223&view=rev
Log:
Merging r243945-243950.
------------------------------------------------------------------------
r243945 | chandlerc | 2015-08-03 20:52:52 -0700 (Mon, 03 Aug 2015) | 5 lines

[UB] Fix two cases of UB in copy/pasted code from SmallVector.

We should really stop copying and pasting code around. =/

Found by UBSan.
------------------------------------------------------------------------

------------------------------------------------------------------------
r243946 | chandlerc | 2015-08-03 20:52:56 -0700 (Mon, 03 Aug 2015) | 9 lines

[UB] Fix the two ways that we would try to memcpy from a null buffer in
the nested name specifier code.

First, skip the entire thing when the input is empty.

Next, handle the case where we started off with a null buffer and a zero
capacity to skip copying and freeing.

This was found with UBSan.
------------------------------------------------------------------------

------------------------------------------------------------------------
r243947 | chandlerc | 2015-08-03 20:52:58 -0700 (Mon, 03 Aug 2015) | 4 lines

[UB] When attaching empty strings to the AST, use an empty StringRef
rather than forcing the bump pointer allocator to produce a viable
pointer. This also fixes UB when we would try to memcpy from the null
incoming StringRef.
------------------------------------------------------------------------

------------------------------------------------------------------------
r243948 | chandlerc | 2015-08-03 20:53:00 -0700 (Mon, 03 Aug 2015) | 9 lines

[UB] Another place where we were trying to put string data into
a BumpPtrAllocator. This at least now handles the case where there is no
concatentation without calling memcpy on a null pointer. It might be
interesting to handle the case where everything is empty without
round-tripping through the allocator, but it wasn't clear to me if the
pointer returned is significant in any way, so I've left it in
a conservatively more-correct state.

Again, found with UBSan.
------------------------------------------------------------------------

------------------------------------------------------------------------
r243949 | chandlerc | 2015-08-03 20:53:01 -0700 (Mon, 03 Aug 2015) | 4 lines

[UB] Guard two calls to memcpy in generated attribute code to handle
null StringRef objects as inputs.

Found by UBSan.
------------------------------------------------------------------------

------------------------------------------------------------------------
r243950 | chandlerc | 2015-08-03 20:53:04 -0700 (Mon, 03 Aug 2015) | 8 lines

[UB] Avoid a really broken call to realloc that would later result in
a bad call to memcpy.

When we only have a buffer from one of the two reparse calls, we can
just return that buffer rather than going through the realloc/memcpy
dance.

Found with UBsan.
------------------------------------------------------------------------

Modified:
    cfe/branches/release_37/   (props changed)
    cfe/branches/release_37/include/clang/AST/ASTVector.h
    cfe/branches/release_37/include/clang/Analysis/Support/BumpVector.h
    cfe/branches/release_37/lib/AST/NestedNameSpecifier.cpp
    cfe/branches/release_37/lib/AST/Stmt.cpp
    cfe/branches/release_37/lib/CodeGen/CGDebugInfo.h
    cfe/branches/release_37/tools/c-index-test/c-index-test.c
    cfe/branches/release_37/utils/TableGen/ClangAttrEmitter.cpp

Propchange: cfe/branches/release_37/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Aug  6 10:53:49 2015
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite:134693-134817
-/cfe/trunk:242244,242285,242293,242297,242313,242382,242422,242499,242574,242600,242660,242662,242667,242678,242766,242854,242905,242973,243018,243048,243085,243098,243101,243105,243144,243153,243196,243206,243277,243280,243285,243289,243343,243417,243463,243538,243594,243642-243644,243964
+/cfe/trunk:242244,242285,242293,242297,242313,242382,242422,242499,242574,242600,242660,242662,242667,242678,242766,242854,242905,242973,243018,243048,243085,243098,243101,243105,243144,243153,243196,243206,243277,243280,243285,243289,243343,243417,243463,243538,243594,243642-243644,243945-243950,243964
 /cfe/trunk/test:170344
 /cfe/trunk/test/SemaTemplate:126920

Modified: cfe/branches/release_37/include/clang/AST/ASTVector.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_37/include/clang/AST/ASTVector.h?rev=244223&r1=244222&r2=244223&view=diff
==============================================================================
--- cfe/branches/release_37/include/clang/AST/ASTVector.h (original)
+++ cfe/branches/release_37/include/clang/AST/ASTVector.h Thu Aug  6 10:53:49 2015
@@ -384,14 +384,15 @@ void ASTVector<T>::grow(const ASTContext
   T *NewElts = new (C, llvm::alignOf<T>()) T[NewCapacity];
 
   // Copy the elements over.
-  if (std::is_class<T>::value) {
-    std::uninitialized_copy(Begin, End, NewElts);
-    // Destroy the original elements.
-    destroy_range(Begin, End);
-  }
-  else {
-    // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove).
-    memcpy(NewElts, Begin, CurSize * sizeof(T));
+  if (Begin != End) {
+    if (std::is_class<T>::value) {
+      std::uninitialized_copy(Begin, End, NewElts);
+      // Destroy the original elements.
+      destroy_range(Begin, End);
+    } else {
+      // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove).
+      memcpy(NewElts, Begin, CurSize * sizeof(T));
+    }
   }
 
   // ASTContext never frees any memory.

Modified: cfe/branches/release_37/include/clang/Analysis/Support/BumpVector.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_37/include/clang/Analysis/Support/BumpVector.h?rev=244223&r1=244222&r2=244223&view=diff
==============================================================================
--- cfe/branches/release_37/include/clang/Analysis/Support/BumpVector.h (original)
+++ cfe/branches/release_37/include/clang/Analysis/Support/BumpVector.h Thu Aug  6 10:53:49 2015
@@ -223,14 +223,15 @@ void BumpVector<T>::grow(BumpVectorConte
   T *NewElts = C.getAllocator().template Allocate<T>(NewCapacity);
   
   // Copy the elements over.
-  if (std::is_class<T>::value) {
-    std::uninitialized_copy(Begin, End, NewElts);
-    // Destroy the original elements.
-    destroy_range(Begin, End);
-  }
-  else {
-    // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove).
-    memcpy(NewElts, Begin, CurSize * sizeof(T));
+  if (Begin != End) {
+    if (std::is_class<T>::value) {
+      std::uninitialized_copy(Begin, End, NewElts);
+      // Destroy the original elements.
+      destroy_range(Begin, End);
+    } else {
+      // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove).
+      memcpy(NewElts, Begin, CurSize * sizeof(T));
+    }
   }
 
   // For now, leak 'Begin'.  We can add it back to a freelist in

Modified: cfe/branches/release_37/lib/AST/NestedNameSpecifier.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_37/lib/AST/NestedNameSpecifier.cpp?rev=244223&r1=244222&r2=244223&view=diff
==============================================================================
--- cfe/branches/release_37/lib/AST/NestedNameSpecifier.cpp (original)
+++ cfe/branches/release_37/lib/AST/NestedNameSpecifier.cpp Thu Aug  6 10:53:49 2015
@@ -435,17 +435,19 @@ TypeLoc NestedNameSpecifierLoc::getTypeL
 namespace {
   void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
               unsigned &BufferCapacity) {
+    if (Start == End)
+      return;
+
     if (BufferSize + (End - Start) > BufferCapacity) {
       // Reallocate the buffer.
-      unsigned NewCapacity 
-      = std::max((unsigned)(BufferCapacity? BufferCapacity * 2 
-                            : sizeof(void*) * 2),
-                 (unsigned)(BufferSize + (End - Start)));
+      unsigned NewCapacity = std::max(
+          (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
+          (unsigned)(BufferSize + (End - Start)));
       char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
-      memcpy(NewBuffer, Buffer, BufferSize);
-      
-      if (BufferCapacity)
+      if (BufferCapacity) {
+        memcpy(NewBuffer, Buffer, BufferSize);
         free(Buffer);
+      }
       Buffer = NewBuffer;
       BufferCapacity = NewCapacity;
     }

Modified: cfe/branches/release_37/lib/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_37/lib/AST/Stmt.cpp?rev=244223&r1=244222&r2=244223&view=diff
==============================================================================
--- cfe/branches/release_37/lib/AST/Stmt.cpp (original)
+++ cfe/branches/release_37/lib/AST/Stmt.cpp Thu Aug  6 10:53:49 2015
@@ -724,6 +724,8 @@ MSAsmStmt::MSAsmStmt(const ASTContext &C
 }
 
 static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
+  if (str.empty())
+    return StringRef();
   size_t size = str.size();
   char *buffer = new (C) char[size];
   memcpy(buffer, str.data(), size);

Modified: cfe/branches/release_37/lib/CodeGen/CGDebugInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_37/lib/CodeGen/CGDebugInfo.h?rev=244223&r1=244222&r2=244223&view=diff
==============================================================================
--- cfe/branches/release_37/lib/CodeGen/CGDebugInfo.h (original)
+++ cfe/branches/release_37/lib/CodeGen/CGDebugInfo.h Thu Aug  6 10:53:49 2015
@@ -484,8 +484,10 @@ private:
   /// are concatenated.
   StringRef internString(StringRef A, StringRef B = StringRef()) {
     char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size());
-    std::memcpy(Data, A.data(), A.size());
-    std::memcpy(Data + A.size(), B.data(), B.size());
+    if (!A.empty())
+      std::memcpy(Data, A.data(), A.size());
+    if (!B.empty())
+      std::memcpy(Data + A.size(), B.data(), B.size());
     return StringRef(Data, A.size() + B.size());
   }
 };

Modified: cfe/branches/release_37/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_37/tools/c-index-test/c-index-test.c?rev=244223&r1=244222&r2=244223&view=diff
==============================================================================
--- cfe/branches/release_37/tools/c-index-test/c-index-test.c (original)
+++ cfe/branches/release_37/tools/c-index-test/c-index-test.c Thu Aug  6 10:53:49 2015
@@ -255,6 +255,17 @@ static int parse_remapped_files_with_try
   if (ret)
     return ret;
 
+  if (num_unsaved_files_no_try_idx == 0) {
+    *unsaved_files = unsaved_files_try_idx;
+    *num_unsaved_files = num_unsaved_files_try_idx;
+    return 0;
+  }
+  if (num_unsaved_files_try_idx == 0) {
+    *unsaved_files = unsaved_files_no_try_idx;
+    *num_unsaved_files = num_unsaved_files_no_try_idx;
+    return 0;
+  }
+
   *num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx;
   *unsaved_files
     = (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx,

Modified: cfe/branches/release_37/utils/TableGen/ClangAttrEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_37/utils/TableGen/ClangAttrEmitter.cpp?rev=244223&r1=244222&r2=244223&view=diff
==============================================================================
--- cfe/branches/release_37/utils/TableGen/ClangAttrEmitter.cpp (original)
+++ cfe/branches/release_37/utils/TableGen/ClangAttrEmitter.cpp Thu Aug  6 10:53:49 2015
@@ -326,7 +326,8 @@ namespace {
       OS << "    " << getLowerName() << "Length = S.size();\n";
       OS << "    this->" << getLowerName() << " = new (C, 1) char ["
          << getLowerName() << "Length];\n";
-      OS << "    std::memcpy(this->" << getLowerName() << ", S.data(), "
+      OS << "    if (!S.empty())\n";
+      OS << "      std::memcpy(this->" << getLowerName() << ", S.data(), "
          << getLowerName() << "Length);\n";
       OS << "  }";
     }
@@ -337,7 +338,8 @@ namespace {
       OS << "A->get" << getUpperName() << "()";
     }
     void writeCtorBody(raw_ostream &OS) const override {
-      OS << "      std::memcpy(" << getLowerName() << ", " << getUpperName()
+      OS << "      if (!" << getUpperName() << ".empty())\n";
+      OS << "        std::memcpy(" << getLowerName() << ", " << getUpperName()
          << ".data(), " << getLowerName() << "Length);";
     }
     void writeCtorInitializers(raw_ostream &OS) const override {




More information about the llvm-branch-commits mailing list