Skip to content

Commit

Permalink
Rollup merge of #81741 - sdroege:zip-trusted-random-access-specializa…
Browse files Browse the repository at this point in the history
…tion-panic-safety, r=KodrAus

Increment `self.index` before calling `Iterator::self.a.__iterator_ge…

…`t_unchecked` in `Zip` `TrustedRandomAccess` specialization

Otherwise if `Iterator::self.a.__iterator_get_unchecked` panics the
index would not have been incremented yet and another call to
`Iterator::next` would read from the same index again, which is not
allowed according to the API contract of `TrustedRandomAccess` for
`!Clone`.

Fixes #81740
  • Loading branch information
Dylan-DPC authored Feb 12, 2021
2 parents 58d72ae + 86a4b27 commit 0cfba2f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions library/core/src/iter/adapters/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,13 @@ where
Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
}
} else if A::may_have_side_effect() && self.index < self.a.size() {
let i = self.index;
self.index += 1;
// match the base implementation's potential side effects
// SAFETY: we just checked that `self.index` < `self.a.len()`
// SAFETY: we just checked that `i` < `self.a.len()`
unsafe {
self.a.__iterator_get_unchecked(self.index);
self.a.__iterator_get_unchecked(i);
}
self.index += 1;
None
} else {
None
Expand Down

0 comments on commit 0cfba2f

Please sign in to comment.