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

MaybeUninit::write_slice_cloned() makes it very easy to accidentally leak #80376

Open
Tracked by #79995
mgeier opened this issue Dec 26, 2020 · 1 comment
Open
Tracked by #79995
Labels
A-raw-pointers Area: raw pointers, MaybeUninit, NonNull A-slice Area: [T]

Comments

@mgeier
Copy link
Contributor

mgeier commented Dec 26, 2020

It is expected behavior that objects assigned/written to MaybeUninit might leak at some point (if assume_init() or similar is not used). However, MaybeUninit::write_slice_cloned() returns a &mut [T] (which might be passed on to further functions, where its origin might not be apparent), and objects assigned to that might leak as well, which I think is less obvious.

playground example

I don't know if there is a way to improve the API to mitigate this problem, but I think at least the documentation should point out the potential leaking.

Currently, it is noted that already initialized values in MaybeUnint will not be dropped:

/// Any already initalized elements will not be dropped.

However, it is not mentioned that assigning to the resulting slice might also lead to leaks.

MaybeUninit::write_slice_cloned() was added in #79607
Tracking issue: #79995
See also https://users.rust-lang.org/t/is-there-a-way-to-copy-t-into-mut-maybeuninit-t-without-unsafe/51301

@beepster4096
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-raw-pointers Area: raw pointers, MaybeUninit, NonNull A-slice Area: [T]
Projects
None yet
Development

No branches or pull requests

3 participants