]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-2470-bounds-check-overflow.rs
Merge commit '6ed6f1e6a1a8f414ba7e6d9b8222e7e5a1686e42' into clippyup
[rust.git] / src / test / ui / issues / issue-2470-bounds-check-overflow.rs
1 // run-fail
2 // error-pattern:index out of bounds
3 // ignore-emscripten no processes
4
5 use std::mem;
6
7 fn main() {
8
9     // This should cause a bounds-check panic, but may not if we do our
10     // bounds checking by comparing the scaled index to the vector's
11     // address-bounds, since we've scaled the index to wrap around to the
12     // address of the 0th cell in the array (even though the index is
13     // huge).
14
15     let x = vec![1_usize, 2_usize, 3_usize];
16
17     let base = x.as_ptr() as usize;
18     let idx = base / mem::size_of::<usize>();
19     println!("ov1 base = 0x{:x}", base);
20     println!("ov1 idx = 0x{:x}", idx);
21     println!("ov1 sizeof::<usize>() = 0x{:x}", mem::size_of::<usize>());
22     println!("ov1 idx * sizeof::<usize>() = 0x{:x}",
23              idx * mem::size_of::<usize>());
24
25     // This should panic.
26     println!("ov1 0x{:x}", x[idx]);
27 }