Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a48f566

Browse files
committedJul 6, 2024
offset_from intrinsic: always allow pointers to point to the same address
1 parent 5569ece commit a48f566

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed
 

‎core/src/ptr/const_ptr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -604,9 +604,9 @@ impl<T: ?Sized> *const T {
604604
///
605605
/// * `self` and `origin` must either
606606
///
607+
/// * point to the same address, or
607608
/// * both be *derived from* a pointer to the same [allocated object], and the memory range between
608-
/// the two pointers must be either empty or in bounds of that object. (See below for an example.)
609-
/// * or both be derived from an integer literal/constant, and point to the same address.
609+
/// the two pointers must be in bounds of that object. (See below for an example.)
610610
///
611611
/// * The distance between the pointers, in bytes, must be an exact multiple
612612
/// of the size of `T`.
@@ -653,14 +653,14 @@ impl<T: ?Sized> *const T {
653653
/// let ptr1 = Box::into_raw(Box::new(0u8)) as *const u8;
654654
/// let ptr2 = Box::into_raw(Box::new(1u8)) as *const u8;
655655
/// let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
656-
/// // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
657-
/// let ptr2_other = (ptr1 as *const u8).wrapping_offset(diff);
656+
/// // Make ptr2_other an "alias" of ptr2.add(1), but derived from ptr1.
657+
/// let ptr2_other = (ptr1 as *const u8).wrapping_offset(diff).wrapping_offset(1);
658658
/// assert_eq!(ptr2 as usize, ptr2_other as usize);
659659
/// // Since ptr2_other and ptr2 are derived from pointers to different objects,
660660
/// // computing their offset is undefined behavior, even though
661-
/// // they point to the same address!
661+
/// // they point to addresses that are in-bounds of the same object!
662662
/// unsafe {
663-
/// let zero = ptr2_other.offset_from(ptr2); // Undefined Behavior
663+
/// let one = ptr2_other.offset_from(ptr2); // Undefined Behavior! ⚠️
664664
/// }
665665
/// ```
666666
#[stable(feature = "ptr_offset_from", since = "1.47.0")]

‎core/src/ptr/mut_ptr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,9 @@ impl<T: ?Sized> *mut T {
829829
///
830830
/// * `self` and `origin` must either
831831
///
832+
/// * point to the same address, or
832833
/// * both be *derived from* a pointer to the same [allocated object], and the memory range between
833-
/// the two pointers must be either empty or in bounds of that object. (See below for an example.)
834-
/// * or both be derived from an integer literal/constant, and point to the same address.
834+
/// the two pointers must be in bounds of that object. (See below for an example.)
835835
///
836836
/// * The distance between the pointers, in bytes, must be an exact multiple
837837
/// of the size of `T`.
@@ -878,14 +878,14 @@ impl<T: ?Sized> *mut T {
878878
/// let ptr1 = Box::into_raw(Box::new(0u8));
879879
/// let ptr2 = Box::into_raw(Box::new(1u8));
880880
/// let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
881-
/// // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
882-
/// let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff);
881+
/// // Make ptr2_other an "alias" of ptr2.add(1), but derived from ptr1.
882+
/// let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff).wrapping_offset(1);
883883
/// assert_eq!(ptr2 as usize, ptr2_other as usize);
884884
/// // Since ptr2_other and ptr2 are derived from pointers to different objects,
885885
/// // computing their offset is undefined behavior, even though
886-
/// // they point to the same address!
886+
/// // they point to addresses that are in-bounds of the same object!
887887
/// unsafe {
888-
/// let zero = ptr2_other.offset_from(ptr2); // Undefined Behavior
888+
/// let one = ptr2_other.offset_from(ptr2); // Undefined Behavior! ⚠️
889889
/// }
890890
/// ```
891891
#[stable(feature = "ptr_offset_from", since = "1.47.0")]

‎core/src/ptr/non_null.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,9 @@ impl<T: ?Sized> NonNull<T> {
735735
///
736736
/// * `self` and `origin` must either
737737
///
738+
/// * point to the same address, or
738739
/// * both be *derived from* a pointer to the same [allocated object], and the memory range between
739-
/// the two pointers must be either empty or in bounds of that object. (See below for an example.)
740-
/// * or both be derived from an integer literal/constant, and point to the same address.
740+
/// the two pointers must be in bounds of that object. (See below for an example.)
741741
///
742742
/// * The distance between the pointers, in bytes, must be an exact multiple
743743
/// of the size of `T`.
@@ -789,14 +789,15 @@ impl<T: ?Sized> NonNull<T> {
789789
/// let ptr1 = NonNull::new(Box::into_raw(Box::new(0u8))).unwrap();
790790
/// let ptr2 = NonNull::new(Box::into_raw(Box::new(1u8))).unwrap();
791791
/// let diff = (ptr2.addr().get() as isize).wrapping_sub(ptr1.addr().get() as isize);
792-
/// // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
793-
/// let ptr2_other = NonNull::new(ptr1.as_ptr().wrapping_byte_offset(diff)).unwrap();
792+
/// // Make ptr2_other an "alias" of ptr2.add(1), but derived from ptr1.
793+
/// let diff_plus_1 = diff.wrapping_add(1);
794+
/// let ptr2_other = NonNull::new(ptr1.as_ptr().wrapping_byte_offset(diff_plus_1)).unwrap();
794795
/// assert_eq!(ptr2.addr(), ptr2_other.addr());
795796
/// // Since ptr2_other and ptr2 are derived from pointers to different objects,
796797
/// // computing their offset is undefined behavior, even though
797-
/// // they point to the same address!
798+
/// // they point to addresses that are in-bounds of the same object!
798799
///
799-
/// let zero = unsafe { ptr2_other.offset_from(ptr2) }; // Undefined Behavior
800+
/// let one = unsafe { ptr2_other.offset_from(ptr2) }; // Undefined Behavior! ⚠️
800801
/// ```
801802
#[inline]
802803
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces

0 commit comments

Comments
 (0)
Failed to load comments.