Skip to content

Commit be1b304

Browse files
authoredAug 28, 2020
Rollup merge of #75943 - elichai:2020-align_offset-docs, r=RalfJung
Fix potential UB in align_offset doc examples Currently it takes a pointer only to the first element in the array, this changes the code to take a pointer to the whole array. miri can't catch this right now because it later calls `x.len()` which re-tags the pointer for the whole array. rust-lang/miri#1526 (comment)
2 parents bc55313 + 0cca597 commit be1b304

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed
 

Diff for: ‎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;

Diff for: ‎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)
Please sign in to comment.