[lld] r264234 - Avoid UB when creating empty atoms. NFC.

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 23 18:16:06 PDT 2016

Author: pete
Date: Wed Mar 23 20:16:06 2016
New Revision: 264234

URL: http://llvm.org/viewvc/llvm-project?rev=264234&view=rev
Avoid UB when creating empty atoms.  NFC.

The stack-size.yaml test had an empty atom content array.  This is
legal, but asking a BumpPtrAllocator for 0 sized data may not be
legal.  Instead just avoid requesting any data when we can just return
an empty ArrayRef instead.


Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp?rev=264234&r1=264233&r2=264234&view=diff
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp Wed Mar 23 20:16:06 2016
@@ -334,6 +334,8 @@ struct MappingTraits<Section> {
       NormalizedFile *file = info->_normalizeMachOFile;
       assert(file != nullptr);
       size_t size = _normalizedContent.size();
+      if (!size)
+        return ArrayRef<uint8_t>();
       uint8_t *bytes = file->ownedAllocations.Allocate<uint8_t>(size);
       std::copy(_normalizedContent.begin(), _normalizedContent.end(), bytes);
       return makeArrayRef(bytes, size);

More information about the llvm-commits mailing list