Skip to content

fix silent overflows on Step impls #36365

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

Merged
merged 11 commits into from
Nov 7, 2016
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());
}