[PATCH] D154708: Fix buffer overflow

Michael Platings via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 7 05:32:25 PDT 2023


michaelplatings created this revision.
michaelplatings added a reviewer: lhames.
Herald added a subscriber: hiraditya.
Herald added a project: All.
michaelplatings requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In practice this wasn't a problem because the overflow was into another
buffer that was immediately overwritten, but the compiler warning was
annoying.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154708

Files:
  llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp


Index: llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
===================================================================
--- llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
+++ llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
@@ -1370,7 +1370,11 @@
     DataSections.push_back({});
     auto &SD = DataSections.back();
     memset(&SD.Sec, 0, sizeof(SD.Sec));
-    strcpy(SD.Sec.sectname, "__objc_imageinfo");
+    // The terminating nul byte doesn't fit into the sectname array so use
+    // memcpy instead of strcpy. (Using strncpy causes a compiler warning).
+    const char SectName[] = "__objc_imageinfo";
+    static_assert(sizeof(SD.Sec.sectname) == sizeof(SectName) - 1);
+    memcpy(SD.Sec.sectname, SectName, sizeof(SD.Sec.sectname));
     strcpy(SD.Sec.segname, "__DATA");
     SD.Sec.size = 8;
     SD.AddFixups = [&](size_t RecordOffset) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154708.538103.patch
Type: text/x-patch
Size: 852 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230707/a7ee00b8/attachment.bin>


More information about the llvm-commits mailing list