Skip to content

Commit 12b8d43

Browse files
committed
avoid integer overflow in ptr-to-int cast
1 parent c6e4f76 commit 12b8d43

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/intptrcast.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::cmp::max;
44

55
use rand::Rng;
66

7-
use rustc_mir::interpret::{AllocId, Pointer, InterpResult, Memory, AllocCheck};
7+
use rustc::ty::layout::HasDataLayout;
8+
use rustc_mir::interpret::{AllocId, Pointer, InterpResult, Memory, AllocCheck, PointerArithmetic};
89
use rustc_target::abi::Size;
910

1011
use crate::{Evaluator, Tag, STACK_ADDR};
@@ -109,7 +110,9 @@ impl<'mir, 'tcx> GlobalState {
109110
};
110111

111112
debug_assert_eq!(base_addr % align.bytes(), 0); // sanity check
112-
Ok(base_addr + ptr.offset.bytes())
113+
// Add offset with the right kind of pointer-overflowing arithmetic.
114+
let dl = memory.data_layout();
115+
Ok(dl.overflowing_offset(base_addr, ptr.offset.bytes()).0)
113116
}
114117

115118
/// Shifts `addr` to make it aligned with `align` by rounding `addr` to the smallest multiple

0 commit comments

Comments
 (0)