Skip to content

Commit f6c4fcf

Browse files
fix silent overflows on Step impls
r? @eddyb
1 parent f1f40f8 commit f6c4fcf

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/libcore/iter/range.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ macro_rules! step_impl_unsigned {
9595
}
9696

9797
#[inline]
98+
#[rustc_inherit_overflow_checks]
9899
fn add_one(&self) -> Self {
99100
*self + 1
100101
}
101102

102103
#[inline]
104+
#[rustc_inherit_overflow_checks]
103105
fn sub_one(&self) -> Self {
104106
*self - 1
105107
}
@@ -166,11 +168,13 @@ macro_rules! step_impl_signed {
166168
}
167169

168170
#[inline]
171+
#[rustc_inherit_overflow_checks]
169172
fn add_one(&self) -> Self {
170173
*self + 1
171174
}
172175

173176
#[inline]
177+
#[rustc_inherit_overflow_checks]
174178
fn sub_one(&self) -> Self {
175179
*self - 1
176180
}
@@ -215,11 +219,13 @@ macro_rules! step_impl_no_between {
215219
}
216220

217221
#[inline]
222+
#[rustc_inherit_overflow_checks]
218223
fn add_one(&self) -> Self {
219224
*self + 1
220225
}
221226

222227
#[inline]
228+
#[rustc_inherit_overflow_checks]
223229
fn sub_one(&self) -> Self {
224230
*self - 1
225231
}

src/libcoretest/iter.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,20 @@ fn test_range_step() {
915915
assert_eq!((isize::MIN..isize::MAX).step_by(1).size_hint(), (usize::MAX, Some(usize::MAX)));
916916
}
917917

918+
#[test]
919+
#[should_panic]
920+
fn test_range_overflow_unsigned() {
921+
let mut it = u8::MAX..;
922+
it.next();
923+
}
924+
925+
#[test]
926+
#[should_panic]
927+
fn test_range_overflow_signed() {
928+
let mut it = i8::MAX..;
929+
it.next();
930+
}
931+
918932
#[test]
919933
fn test_repeat() {
920934
let mut it = repeat(42);

0 commit comments

Comments
 (0)