use MaybeUninit instead of uninitialized#412
Open
arturoc wants to merge 2 commits intokoute:masterfrom
Open
Conversation
Latest versions of rust will panic when trying to leave an uninitialized SerializedValue from mem::uninitialized which was breaking the js! macro
RalfJung
reviewed
Apr 15, 2022
| *(&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(); |
There was a problem hiding this comment.
assume_init should only be called after the tag has also been initialized.
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Latest versions of rust will panic when trying to leave an uninitialized SerializedValue from mem::uninitialized which was breaking the js! macro