-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New lint: mem::replace(mem::uninitialized) is dangerous #4485
Comments
|
There is no migration path from this to a safe usage of MaybeUninit. The only thing compiler warnings can do is prod people to convert this to MaybeUninit followed by Also, the case with mem::zeroed() and a generic parameter still applies even without use of uninitialized memory. |
True, but it has been proposed to basically add |
…lip1995 New lint: mem_replace_with_uninit changelog: add `mem_replace_uninit` lint This fixes rust-lang#4485
New lint: mem_replace_with_uninit changelog: add `mem_replace_uninit` lint This fixes #4485
…lip1995 New lint: mem_replace_with_uninit changelog: add `mem_replace_uninit` lint This fixes rust-lang#4485
New lint: mem_replace_with_uninit changelog: add `mem_replace_uninit` lint This fixes #4485
When working with a
&mut T
sometimes you need to turn it intoT
. This can be achieved usingmem::replace()
:People generally assume that this is safe because the uninitialized memory is never observed. However, this code will drop uninitialized memory if
do_stuff()
panics, because mem::forget will never be reached. This is undefined behavior and potentially a security vulnerability. Example of real-world code with this issue: sile/libflate#35This can be fixed by using the
take_mut
crate as described here. AFAIK there is currently no stdlib solution.Using
mem::zeroed()
instead ofmem::uninitialized()
is also not valid if the target type is not valid for zeroed bit pattern or in case the type is generic.cc @RalfJung
The text was updated successfully, but these errors were encountered: