From 18fe86224eedeb3fb9bcc2fbe13268ec1e054574 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 8 Mar 2024 01:03:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E6=B3=A8=E9=87=8A=EF=BC=8C?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E5=BF=98=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shen-nbt5/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shen-nbt5/src/lib.rs b/shen-nbt5/src/lib.rs index f30f0d2..2d77d95 100644 --- a/shen-nbt5/src/lib.rs +++ b/shen-nbt5/src/lib.rs @@ -21,6 +21,7 @@ macro_rules! read { #[doc = concat!("读取 ", stringify!($ty), " 类型 ", $size, " 长度的数据")] pub fn $name(&mut self) -> $ty { unsafe { + // 使用 std::ptr::read_unaligned 解决未对齐地址问题 let value = std::ptr::read_unaligned(self.data[self.cursor..].as_ptr() as *const $ty); self.cursor += std::mem::size_of::<$ty>(); value.to_be() @@ -31,6 +32,7 @@ macro_rules! read { #[doc = concat!("读取 ", stringify!($ty), " 类型 ", $size, " 长度的数据")] pub fn $name(&mut self) -> $ty { unsafe { + // 使用 std::ptr::read_unaligned 解决未对齐地址问题 let value = std::ptr::read_unaligned(self.data[self.cursor..].as_ptr() as *const $ty); self.cursor += std::mem::size_of::<$ty>(); value