]> git.proxmox.com Git - rustc.git/blame - src/test/run-fail/bug-2470-bounds-check-overflow.rs
Imported Upstream version 0.7
[rustc.git] / src / test / run-fail / bug-2470-bounds-check-overflow.rs
CommitLineData
223e47cc
LB
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
970d7e83
LB
13use std::sys;
14use std::vec;
15
223e47cc
LB
16fn main() {
17
18 // This should cause a bounds-check failure, but may not if we do our
19 // bounds checking by comparing the scaled index to the vector's
20 // address-bounds, since we've scaled the index to wrap around to the
21 // address of the 0th cell in the array (even though the index is
22 // huge).
23
24 let x = ~[1u,2u,3u];
25 do vec::as_imm_buf(x) |p, _len| {
26 let base = p as uint;
27 let idx = base / sys::size_of::<uint>();
28 error!("ov1 base = 0x%x", base);
29 error!("ov1 idx = 0x%x", idx);
30 error!("ov1 sizeof::<uint>() = 0x%x", sys::size_of::<uint>());
31 error!("ov1 idx * sizeof::<uint>() = 0x%x",
32 idx * sys::size_of::<uint>());
33
34 // This should fail.
35 error!("ov1 0x%x", x[idx]);
36 }
37}