Skip to content

Commit 3a986c5

Browse files
fix error for byte_offset()
1 parent a8974ed commit 3a986c5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

library/core/src/ptr/non_null.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,15 @@ impl<T: ?Sized> NonNull<T> {
503503
#[stable(feature = "non_null_convenience", since = "1.80.0")]
504504
#[rustc_const_stable(feature = "non_null_convenience", since = "1.80.0")]
505505
// TODO: add a require to check whether two pointer points to the same allocated object with `same_allocation`
506-
#[ensures(
507-
|result: &isize|
508-
*result == (self.as_ptr() as *const u8).offset_from(origin.as_ptr() as *const u8)
509-
)]
506+
#[ensures(|result: &Self| {
507+
if (count >= 0) {
508+
let offset_ptr = self.as_ptr().byte_add(count as usize) as *mut T;
509+
result.as_ptr() == offset_ptr
510+
} else {
511+
let offset_ptr = self.as_ptr().byte_sub(-count as usize) as *mut T;
512+
result.as_ptr() == offset_ptr
513+
}
514+
})]
510515
pub const unsafe fn byte_offset(self, count: isize) -> Self {
511516
// SAFETY: the caller must uphold the safety contract for `offset` and `byte_offset` has
512517
// the same safety contract.
@@ -1800,6 +1805,7 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
18001805
mod verify {
18011806
use super::*;
18021807
use crate::ptr::null_mut;
1808+
use crate::mem;
18031809

18041810
// pub const unsafe fn new_unchecked(ptr: *mut T) -> Self
18051811
#[kani::proof_for_contract(NonNull::new_unchecked)]
@@ -1864,8 +1870,8 @@ mod verify {
18641870
let arr: [i32; ARR_SIZE] = kani::any();
18651871

18661872
// Randomly generate offsets for the pointers
1867-
let offset = kani::any_where(|x| *x < ARR_SIZE);
1868-
let origin_offset = kani::any_where(|x| *x < ARR_SIZE);
1873+
let offset = kani::any_where(|x| *x <= ARR_SIZE);
1874+
let origin_offset = kani::any_where(|x| *x <= ARR_SIZE);
18691875

18701876
let raw_ptr: *mut i32 = arr.as_ptr() as *mut i32;
18711877
let origin_ptr: *mut i32 = arr.as_ptr() as *mut i32;

0 commit comments

Comments
 (0)