Fix nullptr passed to memcpy in lld/COFF/Chunks.cpp
authorBob Haarman <llvm@inglorion.net>
Fri, 20 Apr 2018 22:16:09 +0000 (22:16 +0000)
committerBob Haarman <llvm@inglorion.net>
Fri, 20 Apr 2018 22:16:09 +0000 (22:16 +0000)
Summary:
ubsan found that we sometimes pass nullptr to memcpy in
SectionChunk::writeTo(). This change adds a check that avoids that.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D45789

llvm-svn: 330490

lld/COFF/Chunks.cpp

index a462caa..6d8a0c7 100644 (file)
@@ -271,7 +271,8 @@ void SectionChunk::writeTo(uint8_t *Buf) const {
     return;
   // Copy section contents from source object file to output file.
   ArrayRef<uint8_t> A = getContents();
-  memcpy(Buf + OutputSectionOff, A.data(), A.size());
+  if (!A.empty())
+    memcpy(Buf + OutputSectionOff, A.data(), A.size());
 
   // Apply relocations.
   size_t InputSize = getSize();