Skip to content

Commit

Permalink
Rollup merge of #82289 - SkiFire13:fix-issue-82282, r=m-ou-se
Browse files Browse the repository at this point in the history
Fix underflow in specialized ZipImpl::size_hint

Fixes #82282
  • Loading branch information
m-ou-se authored Mar 5, 2021
2 parents 20887b7 + aeb4ea7 commit ee796c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion library/core/src/iter/adapters/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}
Expand Down
20 changes: 20 additions & 0 deletions library/core/tests/iter/adapters/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
}
}

0 comments on commit ee796c6

Please sign in to comment.