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

Ryan Mansfield via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 3 06:53:04 PST 2025


================
@@ -285,7 +285,15 @@ void MachOWriter::writeLoadCommands(raw_ostream &OS) {
 
     // Fill remaining bytes with 0. This will only get hit in partially
     // specified test cases.
-    auto BytesRemaining = LC.Data.load_command_data.cmdsize - BytesWritten;
+    // Prevent integer underflow if BytesWritten exceeds cmdsize.
+    if (BytesWritten > LC.Data.load_command_data.cmdsize) {
+      errs() << "warning: load command " << LC.Data.load_command_data.cmd
----------------
rjmansfield wrote:

Ok, sounds good to me. This is more consistent with llvm-readobj's warning too.

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


More information about the llvm-commits mailing list