Commit 5d4401ceec

Jakub Konka <kubkon@jakubkonka.com>
2021-01-19 19:04:01
macho: fix overflowing u64 range
1 parent e726868
Changed files (1)
src
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);