-
Notifications
You must be signed in to change notification settings - Fork 6
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
as_bytes
is unsound
#6
Comments
Could you please provide a quote supporting that reading bytes from padding is invalid? |
This seems to be the best official definition for padding at present https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/glossary.md#padding
So I think if you do
|
I guess you're right. It can only return |
Oh, another option would be returning |
I think I found a clever solution to this problem involving generating const functions that compute alignment padding between each member. Imagine creating a derive macro that generates code like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b309095885a8d96c60f4681727063a04 It's made a lot simpler by the expanded |
I took a stab at implementing this as part of a new crate. It might serve as good inspiration. Instead of using ZSTs aligned with |
It is unsafe to view an arbitrary type as bytes because it might contain uninitialized padding bytes.
zerocopy
solves this with a derive macro but it is a bit limited (it doesn't work with generics, and implementing it manually is discouraged)Unfortunately many types in this crate have padding, so there isn't really a great alternative 😕
The text was updated successfully, but these errors were encountered: