[lld] r195688 - [PECOFF] String pointed by StringRef is not always NUL-terminated.

Rui Ueyama ruiu at google.com
Mon Nov 25 13:33:01 PST 2013


Author: ruiu
Date: Mon Nov 25 15:33:01 2013
New Revision: 195688

URL: http://llvm.org/viewvc/llvm-project?rev=195688&view=rev
Log:
[PECOFF] String pointed by StringRef is not always NUL-terminated.

In order not to overrun a StringRef and copy the trailing garbage, we need to
set the maximum length to be copied by strncpy.

Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=195688&r1=195687&r2=195688&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Mon Nov 25 15:33:01 2013
@@ -726,7 +726,8 @@ SectionChunk::createSectionHeader(String
   // Name field must be NUL-padded. If the name is exactly 8 byte long,
   // there's no terminating NUL.
   std::memset(header.Name, 0, sizeof(header.Name));
-  std::strncpy(header.Name, sectionName.data(), sizeof(header.Name));
+  std::strncpy(header.Name, sectionName.data(),
+               std::min(sizeof(header.Name), sectionName.size()));
 
   header.VirtualSize = 0;
   header.VirtualAddress = 0;





More information about the llvm-commits mailing list