Rollup merge of #68469 - ollie27:skip_count, r=sfackler
Avoid overflow in `std::iter::Skip::count` The call to `count` on the inner iterator can overflow even if `Skip` itself would return less that `usize::max_value()` items. Fixes #68139
This commit is contained in:
commit
e7752aefdc
2 changed files with 16 additions and 2 deletions
|
@ -1815,8 +1815,14 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn count(self) -> usize {
|
fn count(mut self) -> usize {
|
||||||
self.iter.count().saturating_sub(self.n)
|
if self.n > 0 {
|
||||||
|
// nth(n) skips n+1
|
||||||
|
if self.iter.nth(self.n - 1).is_none() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.iter.count()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
8
src/test/ui/iterators/skip-count-overflow.rs
Normal file
8
src/test/ui/iterators/skip-count-overflow.rs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
// run-pass
|
||||||
|
// only-32bit too impatient for 2⁶⁴ items
|
||||||
|
// compile-flags: -C overflow-checks -C opt-level=3
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let i = (0..usize::max_value()).chain(0..10).skip(usize::max_value());
|
||||||
|
assert_eq!(i.count(), 10);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue