Skip to content

The implementation of InPlaceIterable for Peekable is unsound #85322

Closed
@SkiFire13

Description

@SkiFire13

This code leads to an out of bounds write, makes the assert fail and triggers miri:

fn main() {
    let v = vec![0; 5];
    let mut i = v.into_iter().peekable();
    i.peek();
    let v = i.clone().collect::<Vec<_>>();
    assert!(v.len() <= v.capacity());
}

playground

The problem is that after cloning a Peekable the inner iterator can no longer be considered advanced, but the Peekable may still yield the peeked element without advancing it, thus breaking the contract of InPlaceIterable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-criticalCritical priorityT-libsRelevant to the library team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions