[lld] af40bff - [MachO] Fix UB in memcpy

Shoaib Meenai via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 11:34:28 PDT 2020


Author: Shoaib Meenai
Date: 2020-04-28T11:33:54-07:00
New Revision: af40bff32db7840cfbe07278ff0c498604acc5f0

URL: https://github.com/llvm/llvm-project/commit/af40bff32db7840cfbe07278ff0c498604acc5f0
DIFF: https://github.com/llvm/llvm-project/commit/af40bff32db7840cfbe07278ff0c498604acc5f0.diff

LOG: [MachO] Fix UB in memcpy

UBSan complains about a memcpy with a null pointer, so just skip the
memcpy call if the data is empty.

Added: 
    

Modified: 
    lld/MachO/InputSection.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index 76cf8747d28c..8c4a50b6820e 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -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;


        


More information about the llvm-commits mailing list