Skip to content

Commit 7b24d2b

Browse files
committed
debug_assert to ensure that from_raw_parts is only used properly aligned
1 parent db54765 commit 7b24d2b

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

src/libcore/slice/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3878,6 +3878,7 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
38783878
#[inline]
38793879
#[stable(feature = "rust1", since = "1.0.0")]
38803880
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
3881+
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
38813882
Repr { raw: FatPtr { data, len } }.rust
38823883
}
38833884

@@ -3891,6 +3892,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
38913892
#[inline]
38923893
#[stable(feature = "rust1", since = "1.0.0")]
38933894
pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
3895+
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
38943896
Repr { raw: FatPtr { data, len} }.rust_mut
38953897
}
38963898

0 commit comments

Comments
 (0)