[llvm] [yaml2obj][MachO] Fix crash from integer underflow with invalid cmdsize (PR #165924)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 4 04:49:35 PST 2025


================
@@ -300,18 +300,18 @@ void MachOWriter::writeLoadCommands(raw_ostream &OS) {
     // specified test cases.
     // Prevent integer underflow if BytesWritten exceeds cmdsize.
     if (BytesWritten > LC.Data.load_command_data.cmdsize) {
-      const char *name = getLoadCommandName(LC.Data.load_command_data.cmd);
-      if (name)
-        WithColor::warning()
-            << "load command " << i << " " << name << " cmdsize too small ("
-            << LC.Data.load_command_data.cmdsize << " bytes) for actual size ("
-            << BytesWritten << " bytes)\n";
+      std::string Name;
+      const char *NameCStr = getLoadCommandName(LC.Data.load_command_data.cmd);
+      if (NameCStr)
+        Name = NameCStr;
       else
-        WithColor::warning()
-            << "load command " << i << " (0x"
-            << Twine::utohexstr(LC.Data.load_command_data.cmd)
-            << ") cmdsize too small (" << LC.Data.load_command_data.cmdsize
-            << " bytes) for actual size (" << BytesWritten << " bytes)\n";
+        Name = "(0x" + Twine::utohexstr(LC.Data.load_command_data.cmd).str() + ")";
----------------
jh7370 wrote:

I think the norm would be to do `.str()` around the full string concatenation, i.e. something like `("(0x" + Twine::utohexstr(LC.Data.load_command_data.cmd).str() + ")").str()`.

https://github.com/llvm/llvm-project/pull/165924


More information about the llvm-commits mailing list