diff --git a/src/libcore/rt.rs b/src/libcore/rt.rs index 644edb69d5623ef941234d066957afc60804efa7..da598fc3e7f4d0abda95d5562ed3e9a674a535ae 100644 --- a/src/libcore/rt.rs +++ b/src/libcore/rt.rs @@ -40,6 +40,16 @@ fn rt_fail_(expr: *c_char, file: *c_char, line: size_t) { rustrt::rust_upcall_fail(expr, file, line); } +#[rt(fail_bounds_check)] +fn rt_fail_bounds_check(file: *c_char, line: size_t, + index: size_t, len: size_t) { + let msg = fmt!("index out of bounds: the len is %d but the index is %d", + len as int, index as int); + do str::as_buf(msg) |p, _len| { + rt_fail_(p as *c_char, file, line); + } +} + #[rt(exchange_malloc)] fn rt_exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char { return rustrt::rust_upcall_exchange_malloc(td, size); diff --git a/src/rustc/middle/trans/controlflow.rs b/src/rustc/middle/trans/controlflow.rs index 68ebf5fa18960cbe4cee2caa70960b1f1f40d68c..ce32cd0a2dd1e0802855677bec9e22cabb1f7535 100644 --- a/src/rustc/middle/trans/controlflow.rs +++ b/src/rustc/middle/trans/controlflow.rs @@ -344,3 +344,21 @@ fn trans_fail_value(bcx: block, sp_opt: Option, V_fail_str: ValueRef) Unreachable(bcx); return bcx; } + +fn trans_fail_bounds_check(bcx: block, sp: span, + index: ValueRef, len: ValueRef) -> block { + let _icx = bcx.insn_ctxt("trans_fail_bounds_check"); + let ccx = bcx.ccx(); + + let loc = codemap::lookup_char_pos(bcx.sess().parse_sess.cm, sp.lo); + let line = C_int(ccx, loc.line as int); + let filename_cstr = C_cstr(bcx.ccx(), loc.file.name); + let filename = PointerCast(bcx, filename_cstr, T_ptr(T_i8())); + + let args = ~[filename, line, index, len]; + let bcx = callee::trans_rtcall(bcx, ~"fail_bounds_check", args, + expr::Ignore); + Unreachable(bcx); + return bcx; +} + diff --git a/src/rustc/middle/trans/expr.rs b/src/rustc/middle/trans/expr.rs index dafaebef9e01f3fd37f6adc5e681fbb3eb0a7b13..57439daca2f2c527241e22775662d3b5d6dda654 100644 --- a/src/rustc/middle/trans/expr.rs +++ b/src/rustc/middle/trans/expr.rs @@ -946,7 +946,9 @@ fn trans_index(bcx: block, let bounds_check = ICmp(bcx, lib::llvm::IntUGE, scaled_ix, len); let bcx = do with_cond(bcx, bounds_check) |bcx| { - controlflow::trans_fail(bcx, Some(index_expr.span), ~"bounds check") + let unscaled_len = UDiv(bcx, len, vt.llunit_size); + controlflow::trans_fail_bounds_check(bcx, index_expr.span, + ix_val, unscaled_len) }; let elt = InBoundsGEP(bcx, base, ~[ix_val]); let elt = PointerCast(bcx, elt, T_ptr(vt.llunit_ty)); diff --git a/src/test/run-fail/bug-2470-bounds-check-overflow-2.rs b/src/test/run-fail/bug-2470-bounds-check-overflow-2.rs index 0db43856612ad99e01282d026ddb085dfbb65402..ef371d07569cfe02b84311ec2f46445ea051c81f 100644 --- a/src/test/run-fail/bug-2470-bounds-check-overflow-2.rs +++ b/src/test/run-fail/bug-2470-bounds-check-overflow-2.rs @@ -1,5 +1,5 @@ // xfail-test -// error-pattern:bounds check +// error-pattern:index out of bounds fn main() { let x = ~[1u,2u,3u]; @@ -14,4 +14,4 @@ fn main() { // This should fail. error!("ov2 0x%x", x[idx]); -} \ No newline at end of file +} diff --git a/src/test/run-fail/bug-2470-bounds-check-overflow-3.rs b/src/test/run-fail/bug-2470-bounds-check-overflow-3.rs index 949d303eb01aad33d411909a29939fb2fee3b9e2..ae3a9c55b93175d1d0b7e898e736e4bd5917180c 100644 --- a/src/test/run-fail/bug-2470-bounds-check-overflow-3.rs +++ b/src/test/run-fail/bug-2470-bounds-check-overflow-3.rs @@ -1,5 +1,5 @@ // xfail-test -// error-pattern:bounds check +// error-pattern:index out of bounds #[cfg(target_arch="x86")] fn main() { diff --git a/src/test/run-fail/bug-2470-bounds-check-overflow.rs b/src/test/run-fail/bug-2470-bounds-check-overflow.rs index 924b3dda14972986fb7e6979a1e492dd7931993f..fdbcb4de2be2b43964acea189534a15e60e3882c 100644 --- a/src/test/run-fail/bug-2470-bounds-check-overflow.rs +++ b/src/test/run-fail/bug-2470-bounds-check-overflow.rs @@ -1,4 +1,4 @@ -// error-pattern:bounds check +// error-pattern:index out of bounds fn main() { diff --git a/src/test/run-fail/small-negative-indexing.rs b/src/test/run-fail/small-negative-indexing.rs index 96f0c12c760f66d335b513e455b8810c405a2000..5ba6e9dd27abd16a7f6332278e72d801e6be569b 100644 --- a/src/test/run-fail/small-negative-indexing.rs +++ b/src/test/run-fail/small-negative-indexing.rs @@ -1,4 +1,4 @@ -// error-pattern:bounds check +// error-pattern:index out of bounds: the len is 1024 but the index is -1 fn main() { let v = vec::from_fn(1024u, {|n| n}); // this should trip a bounds check diff --git a/src/test/run-fail/str-overrun.rs b/src/test/run-fail/str-overrun.rs index b8bccfe82d4769cea50c50b009d3635bdfc8b555..d9485f3a2896f8c2b4c17b07f3a51326bbfde121 100644 --- a/src/test/run-fail/str-overrun.rs +++ b/src/test/run-fail/str-overrun.rs @@ -1,6 +1,6 @@ // -*- rust -*- -// error-pattern:bounds check +// error-pattern:index out of bounds: the len is 5 but the index is 5 fn main() { let s: ~str = ~"hello"; diff --git a/src/test/run-fail/vec-overrun.rs b/src/test/run-fail/vec-overrun.rs index 8301a05f76bf6eab9107da78fc5a2185b719c03c..fd3254bc6b133d10b1a8777ee6baed573d91b24b 100644 --- a/src/test/run-fail/vec-overrun.rs +++ b/src/test/run-fail/vec-overrun.rs @@ -1,6 +1,6 @@ // -*- rust -*- -// error-pattern:bounds check +// error-pattern:index out of bounds: the len is 1 but the index is 2 fn main() { let v: ~[int] = ~[10]; let x: int = 0; diff --git a/src/test/run-fail/vec-underrun.rs b/src/test/run-fail/vec-underrun.rs index 1228e95ac1e72c68fbb7f4f6d9f422c9cfda37de..88b29471dac1fea651d0e7a6b26abbc256aff26e 100644 --- a/src/test/run-fail/vec-underrun.rs +++ b/src/test/run-fail/vec-underrun.rs @@ -1,6 +1,6 @@ // -*- rust -*- -// error-pattern:bounds check +// error-pattern:index out of bounds: the len is 2 but the index is -1 fn main() { let v: ~[int] = ~[10, 20]; let x: int = 0;