Skip to content

Commit a79a1da

Browse files
authored
Rollup merge of rust-lang#81741 - sdroege:zip-trusted-random-access-specialization-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 rust-lang#81740
2 parents b92d132 + 86a4b27 commit a79a1da

File tree

1 file changed

+4
-3
lines changed
  • library/core/src/iter/adapters

1 file changed

+4
-3
lines changed

library/core/src/iter/adapters/zip.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,13 @@ where
198198
Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
199199
}
200200
} else if A::may_have_side_effect() && self.index < self.a.size() {
201+
let i = self.index;
202+
self.index += 1;
201203
// match the base implementation's potential side effects
202-
// SAFETY: we just checked that `self.index` < `self.a.len()`
204+
// SAFETY: we just checked that `i` < `self.a.len()`
203205
unsafe {
204-
self.a.__iterator_get_unchecked(self.index);
206+
self.a.__iterator_get_unchecked(i);
205207
}
206-
self.index += 1;
207208
None
208209
} else {
209210
None

0 commit comments

Comments
 (0)