Skip to content

Commit

Permalink
Auto merge of #36365 - matthew-piziak:silent-overflow, r=eddyb
Browse files Browse the repository at this point in the history
fix silent overflows on `Step` impls

Part of #36110

r? @eddyb
  • Loading branch information
bors authored Nov 7, 2016
2 parents ec0d1ce + 8f19d5c commit 57f971b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ macro_rules! step_impl_unsigned {

#[inline]
fn add_one(&self) -> Self {
*self + 1
Add::add(*self, 1)
}

#[inline]
fn sub_one(&self) -> Self {
*self - 1
Sub::sub(*self, 1)
}

#[inline]
Expand Down Expand Up @@ -167,12 +167,12 @@ macro_rules! step_impl_signed {

#[inline]
fn add_one(&self) -> Self {
*self + 1
Add::add(*self, 1)
}

#[inline]
fn sub_one(&self) -> Self {
*self - 1
Sub::sub(*self, 1)
}

#[inline]
Expand Down Expand Up @@ -216,12 +216,12 @@ macro_rules! step_impl_no_between {

#[inline]
fn add_one(&self) -> Self {
*self + 1
Add::add(*self, 1)
}

#[inline]
fn sub_one(&self) -> Self {
*self - 1
Sub::sub(*self, 1)
}

#[inline]
Expand Down
29 changes: 29 additions & 0 deletions src/test/run-pass/iter-step-overflow-debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -C debug_assertions=yes

use std::panic;

fn main() {
let r = panic::catch_unwind(|| {
let mut it = u8::max_value()..;
it.next().unwrap(); // 255
it.next().unwrap();
});
assert!(r.is_err());

let r = panic::catch_unwind(|| {
let mut it = i8::max_value()..;
it.next().unwrap(); // 127
it.next().unwrap();
});
assert!(r.is_err());
}
21 changes: 21 additions & 0 deletions src/test/run-pass/iter-step-overflow-ndebug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -C debug_assertions=no

fn main() {
let mut it = u8::max_value()..;
assert_eq!(it.next().unwrap(), 255);
assert_eq!(it.next().unwrap(), u8::min_value());

let mut it = i8::max_value()..;
assert_eq!(it.next().unwrap(), 127);
assert_eq!(it.next().unwrap(), i8::min_value());
}

0 comments on commit 57f971b

Please sign in to comment.