]> granicus.if.org Git - clang/commitdiff
[UB] Fix the two ways that we would try to memcpy from a null buffer in
authorChandler Carruth <chandlerc@gmail.com>
Tue, 4 Aug 2015 03:52:56 +0000 (03:52 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Tue, 4 Aug 2015 03:52:56 +0000 (03:52 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@243946 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/NestedNameSpecifier.cpp

index 50a00502ca9fcace9f5cc949ac99152517161a60..97425d001de0921ef30f5a4d44bb75b2071789fd 100644 (file)
@@ -435,17 +435,19 @@ TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
 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;
     }