Skip to content

Commit

Permalink
fix silent overflows on Step impls
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-piziak committed Sep 9, 2016
1 parent f1f40f8 commit f6c4fcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/libcore/iter/range.rs
Expand Up @@ -95,11 +95,13 @@ macro_rules! step_impl_unsigned {
}

#[inline]
#[rustc_inherit_overflow_checks]
fn add_one(&self) -> Self {
*self + 1
}

#[inline]
#[rustc_inherit_overflow_checks]
fn sub_one(&self) -> Self {
*self - 1
}
Expand Down Expand Up @@ -166,11 +168,13 @@ macro_rules! step_impl_signed {
}

#[inline]
#[rustc_inherit_overflow_checks]
fn add_one(&self) -> Self {
*self + 1
}

#[inline]
#[rustc_inherit_overflow_checks]
fn sub_one(&self) -> Self {
*self - 1
}
Expand Down Expand Up @@ -215,11 +219,13 @@ macro_rules! step_impl_no_between {
}

#[inline]
#[rustc_inherit_overflow_checks]
fn add_one(&self) -> Self {
*self + 1
}

#[inline]
#[rustc_inherit_overflow_checks]
fn sub_one(&self) -> Self {
*self - 1
}
Expand Down
14 changes: 14 additions & 0 deletions src/libcoretest/iter.rs
Expand Up @@ -915,6 +915,20 @@ fn test_range_step() {
assert_eq!((isize::MIN..isize::MAX).step_by(1).size_hint(), (usize::MAX, Some(usize::MAX)));
}

#[test]
#[should_panic]
fn test_range_overflow_unsigned() {
let mut it = u8::MAX..;
it.next();
}

#[test]
#[should_panic]
fn test_range_overflow_signed() {
let mut it = i8::MAX..;
it.next();
}

#[test]
fn test_repeat() {
let mut it = repeat(42);
Expand Down

0 comments on commit f6c4fcf

Please sign in to comment.