From 5d4401ceec60bb9865569e359f923a16657a7304 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 19 Jan 2021 19:04:01 +0100 Subject: [PATCH] macho: fix overflowing u64 range --- src/link/MachO.zig | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 75b9a7b947..afd2ec4eaa 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -2153,8 +2153,13 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, // Is it enough that we could fit this new text block? const sym = self.local_symbols.items[big_block.local_sym_index]; const capacity = big_block.capacity(self.*); - const ideal_capacity = capacity * alloc_num / alloc_den; - const ideal_capacity_end_vaddr = sym.n_value + ideal_capacity; + const ideal_capacity_end_vaddr: u64 = ideal_cap: { + if (math.mul(u64, @divTrunc(capacity, alloc_den), alloc_num)) |cap| { + break :ideal_cap math.add(u64, sym.n_value, cap) catch math.maxInt(u64); + } else |_| { + break :ideal_cap math.maxInt(u64); + } + }; const capacity_end_vaddr = sym.n_value + capacity; const new_start_vaddr_unaligned = capacity_end_vaddr - new_block_ideal_capacity; const new_start_vaddr = mem.alignBackwardGeneric(u64, new_start_vaddr_unaligned, alignment);