Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When a vec/str bounds check fails, include the bad index and the length of the str/vec in the fail message. #3629

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/libcore/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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);
Expand Down
18 changes: 18 additions & 0 deletions src/rustc/middle/trans/controlflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

4 changes: 3 additions & 1 deletion src/rustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,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));
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-fail/bug-2470-bounds-check-overflow-2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// xfail-test
// error-pattern:bounds check
// error-pattern:index out of bounds

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

// This should fail.
error!("ov2 0x%x", x[idx]);
}
}
2 changes: 1 addition & 1 deletion src/test/run-fail/bug-2470-bounds-check-overflow-3.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// xfail-test
// error-pattern:bounds check
// error-pattern:index out of bounds

#[cfg(target_arch="x86")]
fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/bug-2470-bounds-check-overflow.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// error-pattern:bounds check
// error-pattern:index out of bounds

fn main() {

Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/small-negative-indexing.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/str-overrun.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/vec-overrun.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/vec-underrun.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down