Skip to content

Commit be83a12

Browse files
committed
Add some testcases for bug #2470.
1 parent d5d7b3b commit be83a12

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// xfail-test
2+
// error-pattern:bounds check
3+
4+
fn main() {
5+
let x = [1u,2u,3u];
6+
7+
// This should cause a bounds-check failure, but may not if we do our
8+
// bounds checking by comparing a scaled index value to the vector's
9+
// length (in bytes), because the scaling of the index will cause it to
10+
// wrap around to a small number.
11+
12+
let idx = uint::max_value & !(uint::max_value >> 1u);
13+
#error("ov2 idx = 0x%x", idx);
14+
15+
// This should fail.
16+
#error("ov2 0x%x", x[idx]);
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// xfail-test
2+
// error-pattern:bounds check
3+
4+
#[cfg(target_arch="x86")]
5+
fn main() {
6+
let x = [1u,2u,3u];
7+
8+
// This should cause a bounds-check failure, but may not if we do our
9+
// bounds checking by truncating the index value to the size of the
10+
// machine word, losing relevant bits of the index value.
11+
12+
// This test is only meaningful on 32-bit hosts.
13+
14+
let idx = u64::max_value & !(u64::max_value >> 1u);
15+
#error("ov3 idx = 0x%8.8x%8.8x",
16+
(idx >> 32) as uint,
17+
idx as uint);
18+
19+
// This should fail.
20+
#error("ov3 0x%x", x[idx]);
21+
}
22+
23+
#[cfg(target_arch="x86_64")]
24+
fn main() {
25+
// This version just fails anyways, for symmetry on 64-bit hosts.
26+
let x = [1u,2u,3u];
27+
#error("ov3 0x%x", x[200]);
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// error-pattern:bounds check
2+
3+
fn main() {
4+
5+
// This should cause a bounds-check failure, but may not if we do our
6+
// bounds checking by comparing the scaled index to the vector's
7+
// address-bounds, since we've scaled the index to wrap around to the
8+
// address of the 0th cell in the array (even though the index is
9+
// huge).
10+
11+
let x = [1u,2u,3u];
12+
vec::unpack_slice(x) {|p, _len|
13+
let base = p as uint; // base = 0x1230 say
14+
let idx = base / sys::size_of::<uint>(); // idx = 0x0246 say
15+
#error("ov1 base = 0x%x", base);
16+
#error("ov1 idx = 0x%x", idx);
17+
#error("ov1 sizeof::<uint>() = 0x%x", sys::size_of::<uint>());
18+
#error("ov1 idx * sizeof::<uint>() = 0x%x",
19+
idx * sys::size_of::<uint>());
20+
21+
// This should fail.
22+
#error("ov1 0x%x", x[idx]);
23+
}
24+
}

0 commit comments

Comments
 (0)