Skip to content

Commit 1c76d18

Browse files
Dretchbrson
authored andcommitted
When a vec/str bounds check fails, include the bad index and the length of the str/vec in the fail message.
1 parent 2f95f7d commit 1c76d18

File tree

10 files changed

+39
-9
lines changed

10 files changed

+39
-9
lines changed

src/libcore/rt.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ fn rt_fail_(expr: *c_char, file: *c_char, line: size_t) {
4040
rustrt::rust_upcall_fail(expr, file, line);
4141
}
4242

43+
#[rt(fail_bounds_check)]
44+
fn rt_fail_bounds_check(file: *c_char, line: size_t,
45+
index: size_t, len: size_t) {
46+
let msg = fmt!("index out of bounds: the len is %d but the index is %d",
47+
len as int, index as int);
48+
do str::as_buf(msg) |p, _len| {
49+
rt_fail_(p as *c_char, file, line);
50+
}
51+
}
52+
4353
#[rt(exchange_malloc)]
4454
fn rt_exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
4555
return rustrt::rust_upcall_exchange_malloc(td, size);

src/rustc/middle/trans/controlflow.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,21 @@ fn trans_fail_value(bcx: block, sp_opt: Option<span>, V_fail_str: ValueRef)
344344
Unreachable(bcx);
345345
return bcx;
346346
}
347+
348+
fn trans_fail_bounds_check(bcx: block, sp: span,
349+
index: ValueRef, len: ValueRef) -> block {
350+
let _icx = bcx.insn_ctxt("trans_fail_bounds_check");
351+
let ccx = bcx.ccx();
352+
353+
let loc = codemap::lookup_char_pos(bcx.sess().parse_sess.cm, sp.lo);
354+
let line = C_int(ccx, loc.line as int);
355+
let filename_cstr = C_cstr(bcx.ccx(), loc.file.name);
356+
let filename = PointerCast(bcx, filename_cstr, T_ptr(T_i8()));
357+
358+
let args = ~[filename, line, index, len];
359+
let bcx = callee::trans_rtcall(bcx, ~"fail_bounds_check", args,
360+
expr::Ignore);
361+
Unreachable(bcx);
362+
return bcx;
363+
}
364+

src/rustc/middle/trans/expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,9 @@ fn trans_index(bcx: block,
946946

947947
let bounds_check = ICmp(bcx, lib::llvm::IntUGE, scaled_ix, len);
948948
let bcx = do with_cond(bcx, bounds_check) |bcx| {
949-
controlflow::trans_fail(bcx, Some(index_expr.span), ~"bounds check")
949+
let unscaled_len = UDiv(bcx, len, vt.llunit_size);
950+
controlflow::trans_fail_bounds_check(bcx, index_expr.span,
951+
ix_val, unscaled_len)
950952
};
951953
let elt = InBoundsGEP(bcx, base, ~[ix_val]);
952954
let elt = PointerCast(bcx, elt, T_ptr(vt.llunit_ty));

src/test/run-fail/bug-2470-bounds-check-overflow-2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// xfail-test
2-
// error-pattern:bounds check
2+
// error-pattern:index out of bounds
33

44
fn main() {
55
let x = ~[1u,2u,3u];
@@ -14,4 +14,4 @@ fn main() {
1414

1515
// This should fail.
1616
error!("ov2 0x%x", x[idx]);
17-
}
17+
}

src/test/run-fail/bug-2470-bounds-check-overflow-3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// xfail-test
2-
// error-pattern:bounds check
2+
// error-pattern:index out of bounds
33

44
#[cfg(target_arch="x86")]
55
fn main() {

src/test/run-fail/bug-2470-bounds-check-overflow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// error-pattern:bounds check
1+
// error-pattern:index out of bounds
22

33
fn main() {
44

src/test/run-fail/small-negative-indexing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// error-pattern:bounds check
1+
// error-pattern:index out of bounds: the len is 1024 but the index is -1
22
fn main() {
33
let v = vec::from_fn(1024u, {|n| n});
44
// this should trip a bounds check

src/test/run-fail/str-overrun.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// -*- rust -*-
22

3-
// error-pattern:bounds check
3+
// error-pattern:index out of bounds: the len is 5 but the index is 5
44
fn main() {
55
let s: ~str = ~"hello";
66

src/test/run-fail/vec-overrun.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// -*- rust -*-
22

3-
// error-pattern:bounds check
3+
// error-pattern:index out of bounds: the len is 1 but the index is 2
44
fn main() {
55
let v: ~[int] = ~[10];
66
let x: int = 0;

src/test/run-fail/vec-underrun.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// -*- rust -*-
22

3-
// error-pattern:bounds check
3+
// error-pattern:index out of bounds: the len is 2 but the index is -1
44
fn main() {
55
let v: ~[int] = ~[10, 20];
66
let x: int = 0;

0 commit comments

Comments
 (0)