diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h index 407bd9b97020..f0340a9d23f9 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h @@ -185,12 +185,11 @@ packRelocation(const Relocation &r, bool swap, bool isBigEndian) { 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]) {