Skip to content

Unsafe vector overflow #26550

Description

@Veedrac

This gives undefined behaviour due to overflow:

fn main() {
    let one_iter = Some(0).into_iter();
    let big_iter = std::iter::repeat(0).take(usize::max_value());

    one_iter.chain(big_iter).collect::<Vec<_>>();
}

The problem is the overflow on the addition here:

rust/src/libcollections/vec.rs

Lines 1485 to 1489 in a643092

let mut vector = Vec::with_capacity(1 + lower);
unsafe {
ptr::write(vector.get_unchecked_mut(0), element);
vector.set_len(1);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions