diff options
| author | Ralf Jung <post@ralfj.de> | 2022-07-12 09:41:47 -0400 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2022-07-12 10:05:47 -0400 |
| commit | 84ff4da7267bc9fdb3a423a25cb7947333388ead (patch) | |
| tree | 3a531db39eb7c53a22caa21a291462daf211a7bb | |
| parent | b3f4c3119957aa0a250cab08ab586b7a9a680ef1 (diff) | |
| download | rust-84ff4da7267bc9fdb3a423a25cb7947333388ead.tar.gz rust-84ff4da7267bc9fdb3a423a25cb7947333388ead.zip | |
mem::uninitialized: mitigate many incorrect uses of this function
| -rw-r--r-- | library/core/src/lib.rs | 1 | ||||
| -rw-r--r-- | library/core/src/mem/mod.rs | 10 |
2 files changed, 10 insertions, 1 deletions
diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index bd256cec8a1..b4c9f443cac 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -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)] diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index ecd2b75ae44..1e665896a5a 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -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() } } |
