commit 5d4401ceec60bb9865569e359f923a16657a7304 (tree)
parent e726868b020f2dc8b731b7b2ddee1616478cfac8
Author: Jakub Konka <kubkon@jakubkonka.com>
Date: Tue, 19 Jan 2021 19:04:01 +0100
macho: fix overflowing u64 range
Diffstat:
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git 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);