|
182 | 182 | //! [valid]: ptr#safety
|
183 | 183 |
|
184 | 184 | #![stable(feature = "rust1", since = "1.0.0")]
|
| 185 | +#![feature(ub_checks)] |
185 | 186 |
|
186 | 187 | use core::borrow::{Borrow, BorrowMut};
|
187 | 188 | #[cfg(not(no_global_oom_handling))]
|
@@ -1017,7 +1018,7 @@ impl<T: ?Sized> Box<T> {
|
1017 | 1018 | /// resulting `Box`. Specifically, the `Box` destructor will call
|
1018 | 1019 | /// the destructor of `T` and free the allocated memory. For this
|
1019 | 1020 | /// to be safe, the memory must have been allocated in accordance
|
1020 |
| - /// with the [memory layout] used by `Box` . |
| 1021 | + /// with the [memory layout] used by `Box`. |
1021 | 1022 | ///
|
1022 | 1023 | /// # Safety
|
1023 | 1024 | ///
|
@@ -1056,8 +1057,15 @@ impl<T: ?Sized> Box<T> {
|
1056 | 1057 | #[stable(feature = "box_raw", since = "1.4.0")]
|
1057 | 1058 | #[inline]
|
1058 | 1059 | #[must_use = "call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`"]
|
1059 |
| - pub unsafe fn from_raw(raw: *mut T) -> Self { |
1060 |
| - unsafe { Self::from_raw_in(raw, Global) } |
| 1060 | + pub unsafe fn from_raw(ptr: *mut T) -> Self { |
| 1061 | + core::assert_unsafe_precondition!( |
| 1062 | + check_language_ub, |
| 1063 | + "Box::from_raw requires that its pointer argument is properly aligned and not null", |
| 1064 | + () => maybe_is_aligned_and_not_null(ptr as *const (), align_of::<T>(), T::IS_ZST) |
| 1065 | + ); |
| 1066 | + |
| 1067 | + assert_pointer_is_aligned_and_not_null!("Box::from_raw", ptr, align_of::<T>(), T::IS_ZST); |
| 1068 | + unsafe { Self::from_raw_in(ptr, Global) } |
1061 | 1069 | }
|
1062 | 1070 |
|
1063 | 1071 | /// Constructs a box from a `NonNull` pointer.
|
@@ -1111,6 +1119,12 @@ impl<T: ?Sized> Box<T> {
|
1111 | 1119 | #[inline]
|
1112 | 1120 | #[must_use = "call `drop(Box::from_non_null(ptr))` if you intend to drop the `Box`"]
|
1113 | 1121 | pub unsafe fn from_non_null(ptr: NonNull<T>) -> Self {
|
| 1122 | + /*assert_pointer_is_aligned_and_not_null!( |
| 1123 | + "Box::from_non_null", |
| 1124 | + ptr, |
| 1125 | + align_of::<T>(), |
| 1126 | + T::IS_ZST |
| 1127 | + );*/ |
1114 | 1128 | unsafe { Self::from_raw(ptr.as_ptr()) }
|
1115 | 1129 | }
|
1116 | 1130 | }
|
@@ -1166,8 +1180,14 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
|
1166 | 1180 | #[unstable(feature = "allocator_api", issue = "32838")]
|
1167 | 1181 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
1168 | 1182 | #[inline]
|
1169 |
| - pub const unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self { |
1170 |
| - Box(unsafe { Unique::new_unchecked(raw) }, alloc) |
| 1183 | + pub const unsafe fn from_raw_in(ptr: *mut T, alloc: A) -> Self { |
| 1184 | + /*assert_pointer_is_aligned_and_not_null!( |
| 1185 | + "Box::from_raw_in", |
| 1186 | + ptr, |
| 1187 | + align_of::<T>(), |
| 1188 | + T::IS_ZST |
| 1189 | + );*/ |
| 1190 | + Box(unsafe { Unique::new_unchecked(ptr) }, alloc) |
1171 | 1191 | }
|
1172 | 1192 |
|
1173 | 1193 | /// Constructs a box from a `NonNull` pointer in the given allocator.
|
|
0 commit comments