Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use MaybeUninit instead of uninitialized #412

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants