Commit 82144a9073

Jakub Konka <kubkon@jakubkonka.com>
2024-02-07 18:05:20
macho: fix invalid ZigObject size calculation
1 parent 35ac066
Changed files (2)
src/link/MachO/relocatable.zig
@@ -152,7 +152,7 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
 
         // TODO we can avoid reading in the file contents we just wrote if we give the linker
         // ability to write directly to a buffer.
-        try zo.readFileContents(macho_file);
+        try zo.readFileContents(off, macho_file);
     }
 
     var files = std.ArrayList(File.Index).init(gpa);
src/link/MachO/ZigObject.zig
@@ -285,15 +285,9 @@ pub fn checkDuplicates(self: *ZigObject, dupes: anytype, macho_file: *MachO) !vo
 /// This is just a temporary helper function that allows us to re-read what we wrote to file into a buffer.
 /// We need this so that we can write to an archive.
 /// TODO implement writing ZigObject data directly to a buffer instead.
-pub fn readFileContents(self: *ZigObject, macho_file: *MachO) !void {
+pub fn readFileContents(self: *ZigObject, size: usize, macho_file: *MachO) !void {
     const gpa = macho_file.base.comp.gpa;
-    var end_pos: u64 = 0;
-    for (macho_file.segments.items) |seg| {
-        end_pos = @max(end_pos, seg.fileoff + seg.filesize);
-    }
-    const size = std.math.cast(usize, end_pos) orelse return error.Overflow;
     try self.data.resize(gpa, size);
-
     const amt = try macho_file.base.file.?.preadAll(self.data.items, 0);
     if (amt != size) return error.InputOutput;
 }