[lld] r264172 - Copy MachO struct to temporary to avoid unaligned load UB.

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 23 11:00:10 PDT 2016


Author: pete
Date: Wed Mar 23 13:00:10 2016
New Revision: 264172

URL: http://llvm.org/viewvc/llvm-project?rev=264172&view=rev
Log:
Copy MachO struct to temporary to avoid unaligned load UB.

We were already copying this data to a temporary for endian swaps.  Now
we just always copy it, but still only do the endian swaps when needed.

Modified:
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp?rev=264172&r1=264171&r2=264172&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp Wed Mar 23 13:00:10 2016
@@ -380,11 +380,11 @@ readBinary(std::unique_ptr<MemoryBuffer>
             reinterpret_cast<const nlist_64 *>(start + symOffset);
         // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
         for(uint32_t i=0; i < symCount; ++i) {
-          const nlist_64 *sin = &symbols[i];
           nlist_64 tempSym;
-          if (isBig != llvm::sys::IsBigEndianHost) {
-            tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
-          }
+          memcpy(&tempSym, &symbols[i], sizeof(nlist_64));
+          const nlist_64 *sin = &tempSym;
+          if (isBig != llvm::sys::IsBigEndianHost)
+            swapStruct(tempSym);
           Symbol sout;
           if (sin->n_strx > strSize)
             return true;




More information about the llvm-commits mailing list