about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/libcore/mem/mod.rs7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs
index 3fa2b7a2d04..7fcfbf10814 100644
--- a/src/libcore/mem/mod.rs
+++ b/src/libcore/mem/mod.rs
@@ -924,7 +924,12 @@ pub fn drop<T>(_x: T) {}
 #[inline]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
-    ptr::read_unaligned(src as *const T as *const U)
+    // If U has a higher alignment requirement, src may not be suitably aligned.
+    if align_of::<U>() > align_of::<T>() {
+        ptr::read_unaligned(src as *const T as *const U)
+    } else {
+        ptr::read(src as *const T as *const U)
+    }
 }
 
 /// Opaque type representing the discriminant of an enum.