about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGareth Daniel Smith <garethdanielsmith@gmail.com>2012-09-29 12:34:11 +0100
committerBrian Anderson <banderson@mozilla.com>2012-09-30 14:55:56 -0700
commit1c76d189c02ddc6cb6fcf15ae94f3a3ae4de5fa7 (patch)
tree064a5a635790111611b04e678e1c1db90ce00426 /src
parent2f95f7d8de29baca271d04ef89021146061ce975 (diff)
downloadrust-1c76d189c02ddc6cb6fcf15ae94f3a3ae4de5fa7.tar.gz
rust-1c76d189c02ddc6cb6fcf15ae94f3a3ae4de5fa7.zip
When a vec/str bounds check fails, include the bad index and the length of the str/vec in the fail message.
Diffstat (limited to 'src')
-rw-r--r--src/libcore/rt.rs10
-rw-r--r--src/rustc/middle/trans/controlflow.rs18
-rw-r--r--src/rustc/middle/trans/expr.rs4
-rw-r--r--src/test/run-fail/bug-2470-bounds-check-overflow-2.rs4
-rw-r--r--src/test/run-fail/bug-2470-bounds-check-overflow-3.rs2
-rw-r--r--src/test/run-fail/bug-2470-bounds-check-overflow.rs2
-rw-r--r--src/test/run-fail/small-negative-indexing.rs2
-rw-r--r--src/test/run-fail/str-overrun.rs2
-rw-r--r--src/test/run-fail/vec-overrun.rs2
-rw-r--r--src/test/run-fail/vec-underrun.rs2
10 files changed, 39 insertions, 9 deletions
diff --git a/src/libcore/rt.rs b/src/libcore/rt.rs
index 644edb69d56..da598fc3e7f 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 68ebf5fa189..ce32cd0a2dd 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<span>, 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 dafaebef9e0..57439daca2f 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 0db43856612..ef371d07569 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 949d303eb01..ae3a9c55b93 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 924b3dda149..fdbcb4de2be 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 96f0c12c760..5ba6e9dd27a 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 b8bccfe82d4..d9485f3a289 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 8301a05f76b..fd3254bc6b1 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 1228e95ac1e..88b29471dac 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;