Panic-safety issue with Zip
specializations
#137255
Labels
A-iterators
Area: Iterators
A-specialization
Area: Trait impl specialization
C-bug
Category: This is a bug.
I-unsound
Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
P-high
High priority
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
while trying to come up with a more precise formulation of
TrustedRandomAccess
properties, I wonderedsize_hint
’s behavior be on subsequentnext_back
calls?size
by 1 for each call; i.e. length changes at the back vianext_back
are tracked in thesize
information, but__iterator_get_unchecked
has no impact on the sizesize_hint
/size
/len
change whennext_back
is called but panics!?That’s where I took a detour reviewing all the relevant code that uses it. And sure enough, this case wasn’t consistently handled.
E.g. this section
rust/library/core/src/iter/adapters/zip.rs
Lines 374 to 398 in f44efbf
containing in particular
rust/library/core/src/iter/adapters/zip.rs
Lines 386 to 387 in f44efbf
– whilst being conditional’d under logic based on
self.a.size()
–for sure looks like it assumes that a panicking
next_back
call will always at least decrement the size.But – funnily enough – the very same code section lives in a
next_back
impl – at the beginning of that impl – being code that can panic but up to this point nothing happened that would touchself.index
orself.len
, the information inZip
that determinessize_hint
.rust/library/core/src/iter/adapters/zip.rs
Lines 335 to 338 in f44efbf
So okay… this is potentially bad… well… it in fact is bad:
(side-effect simulation code mentioned above)
rust/library/core/src/iter/adapters/zip.rs
Lines 318 to 329 in f44efbf
output:
And here is a playground version that runs this
i.next()
in a longer loop up to segfault@rustbot label T-libs, A-iterators, A-specialization, I-unsound
The text was updated successfully, but these errors were encountered: