Skip to content

Commit 67c1c01

Browse files
committed
offset_from: always allow if both pointers are the same
1 parent 7560748 commit 67c1c01

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

Diff for: library/core/src/ptr/const_ptr.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,9 @@ impl<T: ?Sized> *const T {
627627
/// If any of the following conditions are violated, the result is Undefined
628628
/// Behavior:
629629
///
630-
/// * Both `self` and `origin` must be either in bounds or one
631-
/// byte past the end of the same [allocated object].
632-
///
633-
/// * Both pointers must be *derived from* a pointer to the same object.
634-
/// (See below for an example.)
630+
/// * If `self` and `origin` point to different addresses, then they must both be *derived from*
631+
/// a pointer to the same [allocated object], and they must be either in bounds or one byte
632+
/// past the end of that object. (See below for an example.)
635633
///
636634
/// * The distance between the pointers, in bytes, must be an exact multiple
637635
/// of the size of `T`.
@@ -695,13 +693,13 @@ impl<T: ?Sized> *const T {
695693
/// let ptr2 = Box::into_raw(Box::new(1u8)) as *const u8;
696694
/// let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
697695
/// // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
698-
/// let ptr2_other = (ptr1 as *const u8).wrapping_offset(diff);
696+
/// let ptr2_other = (ptr1 as *const u8).wrapping_offset(diff).wrapping_offset(1);
699697
/// assert_eq!(ptr2 as usize, ptr2_other as usize);
700698
/// // Since ptr2_other and ptr2 are derived from pointers to different objects,
701699
/// // computing their offset is undefined behavior, even though
702-
/// // they point to the same address!
700+
/// // they point to addresses that are in-bounds of the same object!
703701
/// unsafe {
704-
/// let zero = ptr2_other.offset_from(ptr2); // Undefined Behavior
702+
/// let one = ptr2_other.offset_from(ptr2); // Undefined Behavior
705703
/// }
706704
/// ```
707705
#[stable(feature = "ptr_offset_from", since = "1.47.0")]

Diff for: library/core/src/ptr/mut_ptr.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,9 @@ impl<T: ?Sized> *mut T {
801801
/// If any of the following conditions are violated, the result is Undefined
802802
/// Behavior:
803803
///
804-
/// * Both `self` and `origin` must be either in bounds or one
805-
/// byte past the end of the same [allocated object].
806-
///
807-
/// * Both pointers must be *derived from* a pointer to the same object.
808-
/// (See below for an example.)
804+
/// * If `self` and `origin` point to different addresses, then they must both be *derived from*
805+
/// a pointer to the same [allocated object], and they must be either in bounds or one byte
806+
/// past the end of that object. (See below for an example.)
809807
///
810808
/// * The distance between the pointers, in bytes, must be an exact multiple
811809
/// of the size of `T`.
@@ -869,13 +867,13 @@ impl<T: ?Sized> *mut T {
869867
/// let ptr2 = Box::into_raw(Box::new(1u8));
870868
/// let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
871869
/// // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
872-
/// let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff);
870+
/// let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff).wrapping_offset(1);
873871
/// assert_eq!(ptr2 as usize, ptr2_other as usize);
874872
/// // Since ptr2_other and ptr2 are derived from pointers to different objects,
875873
/// // computing their offset is undefined behavior, even though
876-
/// // they point to the same address!
874+
/// // they point to addresses that are in-bounds of the same object!
877875
/// unsafe {
878-
/// let zero = ptr2_other.offset_from(ptr2); // Undefined Behavior
876+
/// let one = ptr2_other.offset_from(ptr2); // Undefined Behavior
879877
/// }
880878
/// ```
881879
#[stable(feature = "ptr_offset_from", since = "1.47.0")]

0 commit comments

Comments
 (0)