Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use &mut [MaybeUninit<T>] in iter::collect #852

Merged
merged 5 commits into from
Apr 29, 2021

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Apr 28, 2021

We create a mutable slice to the uninitialized tail when collecting into
a Vec<T>, but the Rust Reference says it's UB to produce "a reference
or Box<T> that is dangling, unaligned, or points to an invalid value."
UCG#77 hasn't resolved the validity of references to uninitialized
data yet, but we can avoid it by using MaybeUninit<T>.

Fixes #851.

cuviper added 2 commits April 28, 2021 11:36

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
We create a mutable slice to the uninitialized tail when collecting into
a `Vec<T>`, but the Rust Reference says it's UB to produce "a reference
or `Box<T>` that is dangling, unaligned, or points to an invalid value."
[UCG#77] hasn't resolved the validity of references to uninitialized
data yet, but we can avoid it by using `MaybeUninit<T>`.

[UCG#77]: rust-lang/unsafe-code-guidelines#77
@cuviper cuviper force-pushed the maybe-uninit-collect branch from 386a374 to a4796cb Compare April 28, 2021 18:38
Copy link
Member

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. The code is clearer now, I think.

Copy link
Member

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

@nikomatsakis
Copy link
Member

bors r+

@bors bors bot merged commit aacced8 into rayon-rs:master Apr 29, 2021
@cuviper cuviper deleted the maybe-uninit-collect branch February 25, 2023 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UB in Collect:: reserve_get_tail_slice
4 participants