Skip to content

Commit 6a04c76

Browse files
committed
Explicitly test Iterator::position overflows
1 parent af1bfbe commit 6a04c76

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/libcore/iter/traits/iterator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,15 +2069,14 @@ pub trait Iterator {
20692069
Self: Sized,
20702070
P: FnMut(Self::Item) -> bool,
20712071
{
2072-
// The addition might panic on overflow
20732072
#[inline]
2074-
#[rustc_inherit_overflow_checks]
20752073
fn check<T>(
20762074
mut predicate: impl FnMut(T) -> bool,
20772075
) -> impl FnMut(usize, T) -> LoopState<usize, usize> {
2076+
// The addition might panic on overflow
20782077
move |i, x| {
20792078
if predicate(x) { LoopState::Break(i) }
2080-
else { LoopState::Continue(i + 1) }
2079+
else { LoopState::Continue(Add::add(i, 1)) }
20812080
}
20822081
}
20832082

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// run-pass
2+
// only-32bit too impatient for 2⁶⁴ items
3+
// ignore-wasm32-bare compiled with panic=abort by default
4+
// compile-flags: -C debug_assertions=yes
5+
6+
use std::panic;
7+
use std::usize::MAX;
8+
9+
fn main() {
10+
let n = MAX as u64;
11+
assert_eq!((0..).by_ref().position(|i| i >= n), Some(MAX));
12+
13+
let r = panic::catch_unwind(|| {
14+
(0..).by_ref().position(|i| i > n)
15+
});
16+
assert!(r.is_err());
17+
18+
let r = panic::catch_unwind(|| {
19+
(0..=n + 1).by_ref().position(|_| false)
20+
});
21+
assert!(r.is_err());
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-pass
2+
// only-32bit too impatient for 2⁶⁴ items
3+
// compile-flags: -C debug_assertions=no
4+
5+
use std::panic;
6+
use std::usize::MAX;
7+
8+
fn main() {
9+
let n = MAX as u64;
10+
assert_eq!((0..).by_ref().position(|i| i >= n), Some(MAX));
11+
assert_eq!((0..).by_ref().position(|i| i > n), Some(0));
12+
assert_eq!((0..=n + 1).by_ref().position(|_| false), None);
13+
}

0 commit comments

Comments
 (0)