Skip to content

Commit 0cca597

Browse files
committed
Fix potential UB in align_offset docs
1 parent ffd59bf commit 0cca597

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

library/core/src/ptr/const_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ impl<T: ?Sized> *const T {
836836
/// # use std::mem::align_of;
837837
/// # unsafe {
838838
/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
839-
/// let ptr = &x[n] as *const u8;
839+
/// let ptr = x.as_ptr().add(n) as *const u8;
840840
/// let offset = ptr.align_offset(align_of::<u16>());
841841
/// if offset < x.len() - n - 1 {
842842
/// let u16_ptr = ptr.add(offset) as *const u16;

library/core/src/ptr/mut_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ impl<T: ?Sized> *mut T {
10941094
/// # use std::mem::align_of;
10951095
/// # unsafe {
10961096
/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
1097-
/// let ptr = &x[n] as *const u8;
1097+
/// let ptr = x.as_ptr().add(n) as *const u8;
10981098
/// let offset = ptr.align_offset(align_of::<u16>());
10991099
/// if offset < x.len() - n - 1 {
11001100
/// let u16_ptr = ptr.add(offset) as *const u16;

0 commit comments

Comments
 (0)