]> git.lizzy.rs Git - rust.git/blob - src/test/run-fail/bug-2470-bounds-check-overflow.rs
Rollup merge of #34098 - frankmcsherry:patch-1, r=alexcrichton
[rust.git] / src / test / run-fail / bug-2470-bounds-check-overflow.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // error-pattern:index out of bounds
12
13 use std::mem;
14
15 fn main() {
16
17     // This should cause a bounds-check panic, but may not if we do our
18     // bounds checking by comparing the scaled index to the vector's
19     // address-bounds, since we've scaled the index to wrap around to the
20     // address of the 0th cell in the array (even though the index is
21     // huge).
22
23     let x = vec![1_usize, 2_usize, 3_usize];
24
25     let base = x.as_ptr() as usize;
26     let idx = base / mem::size_of::<usize>();
27     println!("ov1 base = 0x{:x}", base);
28     println!("ov1 idx = 0x{:x}", idx);
29     println!("ov1 sizeof::<usize>() = 0x{:x}", mem::size_of::<usize>());
30     println!("ov1 idx * sizeof::<usize>() = 0x{:x}",
31              idx * mem::size_of::<usize>());
32
33     // This should panic.
34     println!("ov1 0x{:x}", x[idx]);
35 }