Skip to content

Commit 82b4356

Browse files
committed
Clarify offset rules a bit
1 parent f191f92 commit 82b4356

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/libcore/intrinsics.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,17 @@ extern "rust-intrinsic" {
255255
/// Returns `true` if a type is managed (will be allocated on the local heap)
256256
pub fn owns_managed<T>() -> bool;
257257

258-
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
259-
/// the object, or one-byte-past-the-end. An arithmetic overflow is also
260-
/// undefined behaviour.
258+
/// Calculates the offset from a pointer.
261259
///
262260
/// This is implemented as an intrinsic to avoid converting to and from an
263261
/// integer, since the conversion would throw away aliasing information.
262+
///
263+
/// # Safety
264+
///
265+
/// Both the starting and resulting pointer must be either in bounds or one
266+
/// byte past the end of an allocated object. If either pointer is out of
267+
/// bounds or arithmetic overflow occurs then any further use of the
268+
/// returned value will result in undefined behavior.
264269
pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
265270

266271
/// Copies `count * size_of<T>` bytes from `src` to `dst`. The source

src/libcore/ptr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,10 @@ impl<T: ?Sized> *const T {
301301
///
302302
/// # Safety
303303
///
304-
/// The offset must be in-bounds of the object, or one-byte-past-the-end.
305-
/// Otherwise `offset` invokes Undefined Behaviour, regardless of whether
306-
/// the pointer is used.
304+
/// Both the starting and resulting pointer must be either in bounds or one
305+
/// byte past the end of an allocated object. If either pointer is out of
306+
/// bounds or arithmetic overflow occurs then
307+
/// any further use of the returned value will result in undefined behavior.
307308
#[stable(feature = "rust1", since = "1.0.0")]
308309
#[inline]
309310
pub unsafe fn offset(self, count: isize) -> *const T where T: Sized {

0 commit comments

Comments
 (0)