diff options
| author | Amanieu d'Antras <amanieu@gmail.com> | 2020-04-22 22:22:48 +0100 |
|---|---|---|
| committer | Amanieu d'Antras <amanieu@gmail.com> | 2020-04-22 22:22:48 +0100 |
| commit | 99de3728f95d1a3fa6063aa4ef2974a45cf35f4c (patch) | |
| tree | a827d6cb934704c9c094876dc97d4cc301a373c0 /src | |
| parent | 82e90d64266b8a4b53935d629786e69610b33f25 (diff) | |
| download | rust-99de3728f95d1a3fa6063aa4ef2974a45cf35f4c.tar.gz rust-99de3728f95d1a3fa6063aa4ef2974a45cf35f4c.zip | |
Only use read_unaligned in transmute_copy if necessary
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/mem/mod.rs | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs index 07f7d28bb75..e42b73ddd23 100644 --- a/src/libcore/mem/mod.rs +++ b/src/libcore/mem/mod.rs @@ -923,7 +923,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. |
