Skip to content

Commit aa93381

Browse files
committed
fix overflow on bounds checks
Closes #9020
1 parent 420b426 commit aa93381

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/librustc/middle/trans/expr.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,6 @@ fn trans_lvalue_unadjusted(bcx: @mut Block, expr: &ast::Expr) -> DatumBlock {
972972

973973
let vt = tvec::vec_types(bcx, base_datum.ty);
974974
base::maybe_name_value(bcx.ccx(), vt.llunit_size, "unit_sz");
975-
let scaled_ix = Mul(bcx, ix_val, vt.llunit_size);
976-
base::maybe_name_value(bcx.ccx(), scaled_ix, "scaled_ix");
977975

978976
let (bcx, base, len) =
979977
base_datum.get_vec_base_and_len(bcx, index_expr.span,
@@ -982,9 +980,9 @@ fn trans_lvalue_unadjusted(bcx: @mut Block, expr: &ast::Expr) -> DatumBlock {
982980
debug2!("trans_index: base {}", bcx.val_to_str(base));
983981
debug2!("trans_index: len {}", bcx.val_to_str(len));
984982

985-
let bounds_check = ICmp(bcx, lib::llvm::IntUGE, scaled_ix, len);
983+
let unscaled_len = UDiv(bcx, len, vt.llunit_size);
984+
let bounds_check = ICmp(bcx, lib::llvm::IntUGE, ix_val, unscaled_len);
986985
let bcx = do with_cond(bcx, bounds_check) |bcx| {
987-
let unscaled_len = UDiv(bcx, len, vt.llunit_size);
988986
controlflow::trans_fail_bounds_check(bcx, index_expr.span,
989987
ix_val, unscaled_len)
990988
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern:index out of bounds: the len is 3 but the index is
12+
13+
use std::uint::max_value;
14+
use std::sys::size_of;
15+
16+
fn main() {
17+
let xs = [1, 2, 3];
18+
xs[max_value / size_of::<int>() + 1];
19+
}

0 commit comments

Comments
 (0)