Skip to content

Commit bd44fb9

Browse files
add in-bound check
1 parent d30ec58 commit bd44fb9

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

library/core/src/ptr/non_null.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,7 @@ impl<T> NonNull<[T]> {
15851585
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
15861586
#[requires(self.as_ptr().cast::<T>().align_offset(core::mem::align_of::<T>()) == 0)] // Ensure the pointer is properly aligned
15871587
#[requires(self.len().checked_mul(core::mem::size_of::<T>()).is_some() && self.len() * core::mem::size_of::<T>() <= isize::MAX as usize)] // Ensure the slice size does not exceed isize::MAX
1588+
#[requires(kani::mem::same_allocation(self.as_ptr() as *const(), self.as_ptr().byte_add(self.len() * core::mem::size_of::<T>()) as *const()))] // Ensure the slice is contained within a single allocation
15881589
#[ensures(|result: &&[MaybeUninit<T>]| result.len() == self.len())] // Length check
15891590
#[ensures(|result: &&[MaybeUninit<T>]| core::ptr::eq(result.as_ptr(), self.cast().as_ptr()))] // Ensure the memory addresses match.
15901591
pub const unsafe fn as_uninit_slice<'a>(self) -> &'a [MaybeUninit<T>] {
@@ -1654,6 +1655,7 @@ impl<T> NonNull<[T]> {
16541655
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
16551656
#[requires(self.as_ptr().cast::<T>().align_offset(core::mem::align_of::<T>()) == 0)] // Ensure the pointer is properly aligned
16561657
#[requires(self.len().checked_mul(core::mem::size_of::<T>()).is_some() && self.len() * core::mem::size_of::<T>() <= isize::MAX as usize)] // Ensure the slice size does not exceed isize::MAX
1658+
#[requires(kani::mem::same_allocation(self.as_ptr() as *const(), self.as_ptr().byte_add(self.len() * core::mem::size_of::<T>()) as *const()))] // Ensure the slice is contained within a single allocation
16571659
#[ensures(|result: &&mut [MaybeUninit<T>]| result.len() == self.len())] // Length check
16581660
#[ensures(|result: &&mut [MaybeUninit<T>]| core::ptr::eq(result.as_ptr(), self.cast().as_ptr()))] // Address check
16591661
pub const unsafe fn as_uninit_slice_mut<'a>(self) -> &'a mut [MaybeUninit<T>] {
@@ -1684,9 +1686,7 @@ impl<T> NonNull<[T]> {
16841686
/// ```
16851687
#[unstable(feature = "slice_ptr_get", issue = "74265")]
16861688
#[inline]
1687-
#[requires(
1688-
ub_checks::can_dereference(self.as_ptr()) // Ensure self can be dereferenced
1689-
)]
1689+
#[requires(ub_checks::can_dereference(self.as_ptr()))] // Ensure self can be dereferenced
16901690
pub unsafe fn get_unchecked_mut<I>(self, index: I) -> NonNull<I::Output>
16911691
where
16921692
I: SliceIndex<[T]>,

0 commit comments

Comments
 (0)