From: Chandler Carruth Date: Tue, 4 Aug 2015 03:48:26 +0000 (+0000) Subject: [UB] Don't allocate space for contained types and then try to copy the X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=1a092e8289d0572ba77aaa769e30896e390d25b9;p=oota-llvm.git [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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243944 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/IR/Type.cpp b/lib/IR/Type.cpp index 1b46b7b01cb..18c2e8c2b48 100644 --- a/lib/IR/Type.cpp +++ b/lib/IR/Type.cpp @@ -420,6 +420,12 @@ void StructType::setBody(ArrayRef Elements, bool isPacked) { if (isPacked) setSubclassData(getSubclassData() | SCDB_Packed); + if (Elements.empty()) { + ContainedTys = nullptr; + NumContainedTys = 0; + return; + } + unsigned NumElements = Elements.size(); Type **Elts = getContext().pImpl->TypeAllocator.Allocate(NumElements); memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements);