From b307ad614c723d8d4967b8c5eabd64f6d947f616 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Sun, 20 Oct 2024 17:10:43 +0200 Subject: [PATCH] Don't allow trivial UB via FieldSlot in safe code cf. https://x.com/qdoit_/status/1848004580115726710, thanks qdoit_ and kupiakos on twitter! --- merde/src/lib.rs | 6 ++---- merde_core/src/deserialize.rs | 5 +++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/merde/src/lib.rs b/merde/src/lib.rs index f7d53e8..9e2e8fd 100644 --- a/merde/src/lib.rs +++ b/merde/src/lib.rs @@ -100,8 +100,7 @@ macro_rules! impl_deserialize { Ok($struct_name { $($field: { if $field.is_none() { - let __field_type_name = std::any::type_name_of_val(&$field); - let __slot = $crate::FieldSlot::new(&mut $field, __field_type_name); + let __slot = $crate::FieldSlot::new(&mut $field); __opinions.default_field_value(stringify!($field), __slot); } $crate::Deserialize::from_option($field, stringify!($field).into())? @@ -163,8 +162,7 @@ macro_rules! impl_deserialize { Ok($struct_name { $($field: { if $field.is_none() { - let __field_type_name = std::any::type_name_of_val(&$field); - let __slot = $crate::FieldSlot::new(&mut $field, __field_type_name); + let __slot = $crate::FieldSlot::new(&mut $field); __opinions.default_field_value(stringify!($field), __slot); } $crate::Deserialize::from_option($field, stringify!($field).into())? diff --git a/merde_core/src/deserialize.rs b/merde_core/src/deserialize.rs index ec9c359..bb101cf 100644 --- a/merde_core/src/deserialize.rs +++ b/merde_core/src/deserialize.rs @@ -317,12 +317,13 @@ pub struct FieldSlot<'s, 'borrow> { impl<'s, 'borrow> FieldSlot<'s, 'borrow> { /// Construct a new `FieldSlot`, ready to be filled #[inline(always)] - pub fn new(option: &'borrow mut Option, type_name_of_slot: &'static str) -> Self { + #[doc(hidden)] + pub fn new(option: &'borrow mut Option) -> Self { Self { option: unsafe { std::mem::transmute::<*mut Option, *mut Option<()>>(option as *mut _) }, - type_name_of_option_field: type_name_of_slot, + type_name_of_option_field: std::any::type_name::>(), _phantom: PhantomData, } }