mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 07:14:28 +00:00
mem::uninitialized: mitigate many incorrect uses of this function
This commit is contained in:
parent
b3f4c31199
commit
84ff4da726
@ -163,6 +163,7 @@
|
||||
#![feature(allow_internal_unstable)]
|
||||
#![feature(associated_type_bounds)]
|
||||
#![feature(auto_traits)]
|
||||
#![feature(cfg_sanitize)]
|
||||
#![feature(cfg_target_has_atomic)]
|
||||
#![feature(cfg_target_has_atomic_equal_alignment)]
|
||||
#![feature(const_fn_floating_point_arithmetic)]
|
||||
|
@ -683,7 +683,15 @@ pub unsafe fn uninitialized<T>() -> T {
|
||||
// SAFETY: the caller must guarantee that an uninitialized value is valid for `T`.
|
||||
unsafe {
|
||||
intrinsics::assert_uninit_valid::<T>();
|
||||
MaybeUninit::uninit().assume_init()
|
||||
let mut val = MaybeUninit::<T>::uninit();
|
||||
|
||||
// Fill memory with 0x01, as an imperfect mitigation for old code that uses this function on
|
||||
// bool, nonnull, and noundef types. But don't do this if we actively want to detect UB.
|
||||
if !cfg!(any(miri, sanitize = "memory")) {
|
||||
val.as_mut_ptr().write_bytes(0x01, 1);
|
||||
}
|
||||
|
||||
val.assume_init()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user