Skip to content

VecDeque implementation creates a slice pointing to possibly uninitialized memory #74189

Closed
@ecstatic-morse

Description

@ecstatic-morse

VecDeque has an internal method called buffer_as_slice, which returns an &[T] containing the entire capacity of the VecDeque. This is undefined behavior if the VecDeque is not full, since some elements of the backing RawVec may be uninitialized. However, this invariant is not documented on buffer_as_slice and is not respected in practice. For example, VecDeque::iter calls buffer_as_slice unconditionally:

#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter(&self) -> Iter<'_, T> {
Iter { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_slice() } }
}

This one seems so obvious that I'm wondering if I've overlooked something. cc @rust-lang/wg-unsafe-code-guidelines

Found while doing #74172.

Metadata

Metadata

Assignees

Labels

A-collectionsArea: `std::collections`C-bugCategory: This is a bug.E-help-wantedCall for participation: Help is requested to fix this issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions