Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/export: Replace unsound
to_padded_byte_vector()
implementa…
…tion with `bytemuck`. The function `to_padded_byte_vector()` is unsound because: * It accepts an arbitrary `Vec<T>` without checking that `T` contains no padding, which is UB to read in any way including by reinterpreting as `u8`s. * It produces a `Vec` which thinks it has a different alignment than the allocation was actually created with. To fix these problems, this change: * Uses `bytemuck` to check the no-padding condition. * Creates a new `Vec` instead of trying to reuse the existing one. (Conditional reuse would be possible, but more complex.) An alternative to `bytemuck` would be to make `to_padded_byte_vector()` an `unsafe fn` (or to accept `Vertex` only instead of `T`). However, I think it is valuable to demonstrate how to do this conversion using safe tools, to encourage people to use safe code instead of writing unsafe code without fully understanding the requirements.
- Loading branch information