Rollup merge of #82289 - SkiFire13:fix-issue-82282, r=m-ou-se

Fix underflow in specialized ZipImpl::size_hint

Fixes #82282
This commit is contained in:
Mara 2021-03-05 10:57:19 +01:00 committed by GitHub
commit ee796c6523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -200,6 +200,7 @@ where
} else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a.size() {
let i = self.index;
self.index += 1;
self.len += 1;
// match the base implementation's potential side effects
// SAFETY: we just checked that `i` < `self.a.len()`
unsafe {
@ -258,7 +259,7 @@ where
if sz_a != sz_b {
let sz_a = self.a.size();
if A::MAY_HAVE_SIDE_EFFECT && sz_a > self.len {
for _ in 0..sz_a - cmp::max(self.len, self.index) {
for _ in 0..sz_a - self.len {
self.a.next_back();
}
}

View File

@ -245,3 +245,23 @@ fn test_double_ended_zip() {
assert_eq!(it.next_back(), Some((3, 3)));
assert_eq!(it.next(), None);
}
#[test]
fn test_issue_82282() {
fn overflowed_zip(arr: &[i32]) -> impl Iterator<Item = (i32, &())> {
static UNIT_EMPTY_ARR: [(); 0] = [];
let mapped = arr.into_iter().map(|i| *i);
let mut zipped = mapped.zip(UNIT_EMPTY_ARR.iter());
zipped.next();
zipped
}
let arr = [1, 2, 3];
let zip = overflowed_zip(&arr).zip(overflowed_zip(&arr));
assert_eq!(zip.size_hint(), (0, Some(0)));
for _ in zip {
panic!();
}
}