[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.

llvm-svn: 243944
This commit is contained in:
Chandler Carruth 2015-08-04 03:48:26 +00:00
parent 3874ee6869
commit 7771197955
1 changed files with 6 additions and 0 deletions

View File

@ -420,6 +420,12 @@ void StructType::setBody(ArrayRef<Type*> 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<Type*>(NumElements);
memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements);