Use read_unaligned when reading u64#257
Conversation
Sorry to jump into this PR, but I am not sure I follow the reason for this being undefined behaviour. The runtime serialization guarantees that the input pointer will be aligned to an |
No worries - your comment is welcome :) Yes, if we include the ABI behavior in the calculation, then these reads are actually safe. I read this tutorial up to this point, and while it is possible that I've missed it, I did not see the VM ABI alignment behavior stated anywhere. So if I consider just the content of the tutorial, it is unsafe to do an unaligned read here. |
Casting an arbitrary pointer to `u64` is an undefined behavior. Not sure if casting of the discriminator value is really necessary, as it is just a `u8` in this example. But using the same `*ptr.cast::<uXX>()` pattern in there seems wrong. It is a noop for `u8`, but if a `u64` is used, it becomes undefined behavior as well. Also, the `unsafe` usage is somewhat unclear. Dereferencing a pointer is an unsafe operation, but the first two examples do not have `unsafe`. Yet in the last example, when a slice is constructed, `unsafe` is present. I assume that the first two examples are expected to be in an unsafe context already?
Casting an arbitrary pointer to
u64is an undefined behavior.Not sure if casting of the discriminator value is really necessary, as it is just a
u8in this example. But using the same*ptr.cast::<uXX>()pattern in there seems wrong. It is a noop foru8, but if au64is used, it becomes undefined behavior as well.Also, the
unsafeusage is somewhat unclear. Dereferencing a pointer is an unsafe operation, but the first two examples do not haveunsafe. Yet in the last example, when a slice is constructed,unsafeis present. I assume that the first two examples are expected to be in an unsafe context already?