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