[lld][macho] Fix gcc category merging warning (#86091)
Fixing gcc warning regarding creating non-null-terminated string:
```
../../lld/MachO/ObjC.cpp:1226:10: warning: 'char* strncpy(char*, const char*, size_t)' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]
1226 | strncpy(strData, str, len);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
../../lld/MachO/ObjC.cpp: In member function 'void {anonymous}::ObjcCategoryMerger::emitAndLinkPointerList(lld::macho::Defined*, uint32_t, const {anonymous}::ObjcCategoryMerger::ClassExtensionInfo&, const {anonymous}::ObjcCategoryMerger::PointerListInfo&)':
../../lld/MachO/ObjC.cpp:1223:24: note: length computed here
1223 | uint32_t len = strlen(str);
| ~~~~~~^~~~~
```
This is not actually a bug, as `newSectionData` returns a
zero-initialized memory region, so the null terminator will be there.
GitOrigin-RevId: e4a672ef85f76c3402b81640e1e83e5d3069d1b9
diff --git a/MachO/ObjC.cpp b/MachO/ObjC.cpp
index 66959cf..5902b82 100644
--- a/MachO/ObjC.cpp
+++ b/MachO/ObjC.cpp
@@ -1221,9 +1221,11 @@
StringRef ObjcCategoryMerger::newStringData(const char *str) {
uint32_t len = strlen(str);
- auto &data = newSectionData(len + 1);
+ uint32_t bufSize = len + 1;
+ auto &data = newSectionData(bufSize);
char *strData = reinterpret_cast<char *>(data.data());
- strncpy(strData, str, len);
+ // Copy the string chars and null-terminator
+ memcpy(strData, str, bufSize);
return StringRef(strData, len);
}