diff options
author | Chandler Carruth <chandlerc@gmail.com> | 2015-08-04 03:48:26 +0000 |
---|---|---|
committer | Chandler Carruth <chandlerc@gmail.com> | 2015-08-04 03:48:26 +0000 |
commit | 7771197955dd06dd1b9f9653fa731e9ec9060b83 (patch) | |
tree | 5a7fcd341f4ca58cd502bf583da1c2787190e407 | |
parent | 3874ee6869e47fe714d98d8771bc09970a1076e2 (diff) | |
download | bcm5719-llvm-7771197955dd06dd1b9f9653fa731e9ec9060b83.tar.gz bcm5719-llvm-7771197955dd06dd1b9f9653fa731e9ec9060b83.zip |
[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
-rw-r--r-- | llvm/lib/IR/Type.cpp | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp index 1b46b7b01cb..18c2e8c2b48 100644 --- a/llvm/lib/IR/Type.cpp +++ b/llvm/lib/IR/Type.cpp @@ -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); |