[MachO] Fix UB in memcpy
authorShoaib Meenai <smeenai@fb.com>
Tue, 28 Apr 2020 18:29:30 +0000 (11:29 -0700)
committerShoaib Meenai <smeenai@fb.com>
Tue, 28 Apr 2020 18:33:54 +0000 (11:33 -0700)
UBSan complains about a memcpy with a null pointer, so just skip the
memcpy call if the data is empty.

lld/MachO/InputSection.cpp

index 76cf874..8c4a50b 100644 (file)
@@ -26,7 +26,8 @@ uint64_t InputSection::getFileOffset() const {
 }
 
 void InputSection::writeTo(uint8_t *buf) {
-  memcpy(buf, data.data(), data.size());
+  if (!data.empty())
+    memcpy(buf, data.data(), data.size());
 
   for (Reloc &r : relocs) {
     uint64_t va = 0;