Skip to content

Conversation

@arturoc
Copy link

@arturoc arturoc commented Nov 23, 2020

Latest versions of rust will panic when trying to leave an uninitialized SerializedValue from mem::uninitialized which was breaking the js! macro

Latest versions of rust will panic when trying to leave an uninitialized SerializedValue from mem::uninitialized which was breaking the js! macro
*(&mut value as *mut SerializedValue as *mut $untagged_type) = untagged;
let mut value = mem::MaybeUninit::<SerializedValue>::uninit();
*(value.as_mut_ptr() as *mut $untagged_type) = untagged;
let mut value = value.assume_init();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assume_init should only be called after the tag has also been initialized.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this is still wrong for many cases -- the assignment of untagged might fail to completely initialize both data_1 and data_2.

And finally, if untagged is a pointer type, then this would transmute a pointer to an integer, which is not something one should do. SerializedValue seems to be intended as a type that can hold arbitrary data, so u64 and u32 are the wrong types -- those types are specifically for integers, not for arbitrary data. Use e.g. MaybeUninit<u64> for arbitrary data of size 64 bits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants