From 226c662da6033846669661ef6fa644ce84e1ef2b Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Tue, 11 Jul 2023 08:07:47 -0600 Subject: [PATCH] fix: Use unaligned read as copy_nonoverlapping requires alignment (#1278) --- src/fast_api.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/fast_api.rs b/src/fast_api.rs index ebfa389c..8dfb28d5 100644 --- a/src/fast_api.rs +++ b/src/fast_api.rs @@ -234,14 +234,12 @@ impl FastApiOneByteString { } impl FastApiTypedArray { + /// Performs an unaligned-safe read of T from the underlying data. #[inline(always)] pub fn get(&self, index: usize) -> T { debug_assert!(index < self.length); - let mut t: T = Default::default(); - unsafe { - ptr::copy_nonoverlapping(self.data.add(index), &mut t, 1); - } - t + // SAFETY: src is valid for reads, and is a valid value for T + unsafe { ptr::read_unaligned(self.data.add(index)) } } #[inline(always)]