[PATCH] D51547: MachO: Fix out-of-bounds memory access in getString16

Tom Stellard via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 7 08:45:57 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL341670: MachO: Fix out-of-bounds memory access in getString16 (authored by tstellar, committed by ).

Repository:
  rL LLVM

https://reviews.llvm.org/D51547

Files:
  lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h


Index: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h
===================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h
@@ -185,12 +185,11 @@
   return result;
 }
 
-inline StringRef getString16(const char s[16]) {
-  StringRef x = s;
-  if ( x.size() > 16 )
-    return x.substr(0, 16);
-  else
-    return x;
+static StringRef getString16(const char s[16]) {
+  // The StringRef(const char *) constructor passes the const char * to
+  // strlen(), so we can't use this constructor here, because if there is no
+  // null terminator in s, then strlen() will read past the end of the array.
+  return StringRef(s, strnlen(s, 16));
 }
 
 inline void setString16(StringRef str, char s[16]) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51547.164437.patch
Type: text/x-patch
Size: 851 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180907/58298583/attachment.bin>


More information about the llvm-commits mailing list