authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-07 18:05:20+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-07 19:27:26+01:00
log82144a9073aa06f037e248f332608f94caed59b9
treee2c94442c45b34a8119d2821d71296ed731e2168
parent35ac066f1d550445519aceb098d7e3f517f98c55

macho: fix invalid ZigObject size calculation


2 files changed, 2 insertions(+), 8 deletions(-)

src/link/MachO/ZigObject.zig+1-7
......@@ -285,15 +285,9 @@ pub fn checkDuplicates(self: *ZigObject, dupes: anytype, macho_file: *MachO) !vo
285285/// This is just a temporary helper function that allows us to re-read what we wrote to file into a buffer.
286286/// We need this so that we can write to an archive.
287287/// TODO implement writing ZigObject data directly to a buffer instead.
288pub fn readFileContents(self: *ZigObject, macho_file: *MachO) !void {
288pub fn readFileContents(self: *ZigObject, size: usize, macho_file: *MachO) !void {
289289 const gpa = macho_file.base.comp.gpa;
290 var end_pos: u64 = 0;
291 for (macho_file.segments.items) |seg| {
292 end_pos = @max(end_pos, seg.fileoff + seg.filesize);
293 }
294 const size = std.math.cast(usize, end_pos) orelse return error.Overflow;
295290 try self.data.resize(gpa, size);
296
297291 const amt = try macho_file.base.file.?.preadAll(self.data.items, 0);
298292 if (amt != size) return error.InputOutput;
299293}
src/link/MachO/relocatable.zig+1-1
......@@ -152,7 +152,7 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
152152
153153 // TODO we can avoid reading in the file contents we just wrote if we give the linker
154154 // ability to write directly to a buffer.
155 try zo.readFileContents(macho_file);
155 try zo.readFileContents(off, macho_file);
156156 }
157157
158158 var files = std.ArrayList(File.Index).init(gpa);