[llvm] r243944 - [UB] Don't allocate space for contained types and then try to copy the

Chandler Carruth chandlerc at gmail.com
Mon Aug 3 20:48:26 PDT 2015


Author: chandlerc
Date: Mon Aug  3 22:48:26 2015
New Revision: 243944

URL: http://llvm.org/viewvc/llvm-project?rev=243944&view=rev
Log:
[UB] Don't allocate space for contained types and then try to copy the
contained types into the space when we have no contained types. This
fixes the UB stemming from a call to memcpy with a null pointer. This
also reduces the calls to allocate because this actually happens in
a notable client - Clang.

Found by UBSan.

Modified:
    llvm/trunk/lib/IR/Type.cpp

Modified: llvm/trunk/lib/IR/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Type.cpp?rev=243944&r1=243943&r2=243944&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Type.cpp (original)
+++ llvm/trunk/lib/IR/Type.cpp Mon Aug  3 22:48:26 2015
@@ -420,6 +420,12 @@ void StructType::setBody(ArrayRef<Type*>
   if (isPacked)
     setSubclassData(getSubclassData() | SCDB_Packed);
 
+  if (Elements.empty()) {
+    ContainedTys = nullptr;
+    NumContainedTys = 0;
+    return;
+  }
+
   unsigned NumElements = Elements.size();
   Type **Elts = getContext().pImpl->TypeAllocator.Allocate<Type*>(NumElements);
   memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements);





More information about the llvm-commits mailing list