From 7b24d2bb351efeea9c0109d8a662b1038fe8f539 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 2 Aug 2018 11:33:59 +0200 Subject: [PATCH] debug_assert to ensure that from_raw_parts is only used properly aligned --- src/libcore/slice/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index a4dde38cb7b..eeb171ebb9b 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -3878,6 +3878,7 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] { + debug_assert!(data as usize % mem::align_of::() == 0, "attempt to create unaligned slice"); Repr { raw: FatPtr { data, len } }.rust } @@ -3891,6 +3892,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] { + debug_assert!(data as usize % mem::align_of::() == 0, "attempt to create unaligned slice"); Repr { raw: FatPtr { data, len} }.rust_mut }