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