From 09c256d5eb925d06d7fc9352ddbf99abf98fba3d Mon Sep 17 00:00:00 2001 From: Will Glozer Date: Sat, 27 Aug 2022 21:35:32 +0900 Subject: serde_v8: fix pointer size assumptions (#15613) --- serde_v8/magic/bytestring.rs | 9 ++++++--- serde_v8/magic/transl8.rs | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'serde_v8') diff --git a/serde_v8/magic/bytestring.rs b/serde_v8/magic/bytestring.rs index c7c1b9de8..5580fdced 100644 --- a/serde_v8/magic/bytestring.rs +++ b/serde_v8/magic/bytestring.rs @@ -5,6 +5,8 @@ use crate::Error; use smallvec::SmallVec; use std::mem::size_of; +const USIZE2X: usize = size_of::() * 2; + #[derive( PartialEq, Eq, @@ -18,14 +20,15 @@ use std::mem::size_of; )] #[as_mut(forward)] #[as_ref(forward)] -pub struct ByteString(SmallVec<[u8; 16]>); +pub struct ByteString(SmallVec<[u8; USIZE2X]>); impl_magic!(ByteString); -// const-assert that Vec and SmallVec<[u8; 16]> have a same size. +// const-assert that Vec and SmallVec<[u8; size_of::() * 2]> have a same size. // Note from https://docs.rs/smallvec/latest/smallvec/#union - // smallvec can still be larger than Vec if the inline buffer is // larger than two machine words. -const _: () = assert!(size_of::>() == size_of::>()); +const _: () = + assert!(size_of::>() == size_of::>()); impl ToV8 for ByteString { fn to_v8<'a>( diff --git a/serde_v8/magic/transl8.rs b/serde_v8/magic/transl8.rs index d08be468b..2336b5b27 100644 --- a/serde_v8/magic/transl8.rs +++ b/serde_v8/magic/transl8.rs @@ -105,13 +105,13 @@ pub(crate) unsafe fn opaque_recv(ptr: &T) -> u64 { /// Transmutes an "opaque" ptr back into a reference pub(crate) unsafe fn opaque_deref<'a, T>(ptr: u64) -> &'a T { - std::mem::transmute(ptr) + std::mem::transmute(ptr as usize) } /// Transmutes & copies the value from the "opaque" ptr /// NOTE: takes ownership & requires other end to forget its ownership pub(crate) unsafe fn opaque_take(ptr: u64) -> T { - std::mem::transmute_copy::(std::mem::transmute(ptr)) + std::mem::transmute_copy::(std::mem::transmute(ptr as usize)) } macro_rules! impl_magic { -- cgit v1.2.3