Skip to content

Commit 724cf41

Browse files
committed
use checked arithmetic in intrptrcast
1 parent d5ca345 commit 724cf41

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/intptrcast.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ impl<'mir, 'tcx> GlobalState {
9595
rng.gen_range(0, 16)
9696
};
9797
// From next_base_addr + slack, round up to adjust for alignment.
98-
let base_addr = Self::align_addr(global_state.next_base_addr + slack, align.bytes());
98+
let base_addr = global_state.next_base_addr.checked_add(slack).unwrap();
99+
let base_addr = Self::align_addr(base_addr, align.bytes());
99100
entry.insert(base_addr);
100101
trace!(
101102
"Assigning base address {:#x} to allocation {:?} (slack: {}, align: {})",
@@ -104,7 +105,7 @@ impl<'mir, 'tcx> GlobalState {
104105

105106
// Remember next base address. If this allocation is zero-sized, leave a gap
106107
// of at least 1 to avoid two allocations having the same base address.
107-
global_state.next_base_addr = base_addr + max(size.bytes(), 1);
108+
global_state.next_base_addr = base_addr.checked_add(max(size.bytes(), 1)).unwrap();
108109
// Given that `next_base_addr` increases in each allocation, pushing the
109110
// corresponding tuple keeps `int_to_ptr_map` sorted
110111
global_state.int_to_ptr_map.push((base_addr, ptr.alloc_id));
@@ -124,7 +125,7 @@ impl<'mir, 'tcx> GlobalState {
124125
fn align_addr(addr: u64, align: u64) -> u64 {
125126
match addr % align {
126127
0 => addr,
127-
rem => addr + align - rem
128+
rem => addr.checked_add(align).unwrap() - rem
128129
}
129130
}
130131
}

0 commit comments

Comments
 (0)