diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2025-01-10 13:00:45 -0800 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2025-01-15 22:17:57 -0800 |
| commit | c18718c9c28cc4c700312775c2b7f87cd79d3063 (patch) | |
| tree | 7a2b8e3faab3a4a953a2f6af19c7afe6d27f2397 /library/core/src/ptr/mod.rs | |
| parent | 5cd16b7f2bc3624f2d658aa87151279878d2652a (diff) | |
| download | rust-c18718c9c28cc4c700312775c2b7f87cd79d3063.tar.gz rust-c18718c9c28cc4c700312775c2b7f87cd79d3063.zip | |
Less unsafe in `dangling`/`without_provenance`
Diffstat (limited to 'library/core/src/ptr/mod.rs')
| -rw-r--r-- | library/core/src/ptr/mod.rs | 11 |
1 files changed, 3 insertions, 8 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index f58c0e12411..e1348552b65 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -596,12 +596,7 @@ pub const fn null_mut<T: ?Sized + Thin>() -> *mut T { #[stable(feature = "strict_provenance", since = "1.84.0")] #[rustc_const_stable(feature = "strict_provenance", since = "1.84.0")] pub const fn without_provenance<T>(addr: usize) -> *const T { - // An int-to-pointer transmute currently has exactly the intended semantics: it creates a - // pointer without provenance. Note that this is *not* a stable guarantee about transmute - // semantics, it relies on sysroot crates having special status. - // SAFETY: every valid integer is also a valid pointer (as long as you don't dereference that - // pointer). - unsafe { mem::transmute(addr) } + without_provenance_mut(addr) } /// Creates a new pointer that is dangling, but non-null and well-aligned. @@ -618,7 +613,7 @@ pub const fn without_provenance<T>(addr: usize) -> *const T { #[stable(feature = "strict_provenance", since = "1.84.0")] #[rustc_const_stable(feature = "strict_provenance", since = "1.84.0")] pub const fn dangling<T>() -> *const T { - without_provenance(mem::align_of::<T>()) + dangling_mut() } /// Creates a pointer with the given address and no [provenance][crate::ptr#provenance]. @@ -661,7 +656,7 @@ pub const fn without_provenance_mut<T>(addr: usize) -> *mut T { #[stable(feature = "strict_provenance", since = "1.84.0")] #[rustc_const_stable(feature = "strict_provenance", since = "1.84.0")] pub const fn dangling_mut<T>() -> *mut T { - without_provenance_mut(mem::align_of::<T>()) + NonNull::dangling().as_ptr() } /// Converts an address back to a pointer, picking up some previously 'exposed' |
