Fix read_pixels_of_type UB: use bytemuck::cast_slice#918
Open
kimjune01 wants to merge 1 commit into
Open
Conversation
The previous implementation used Vec::from_raw_parts to reinterpret a Vec<u8> as Vec<T>, then mem::forget to suppress the original Vec's destructor. This was UB for two reasons: 1. mem::forget receives an invalid value (already-transferred ownership) 2. Vec<T> deallocates with align_of::<T>(), but the memory was allocated with align_of::<u8>() = 1, violating alloc/dealloc layout invariants Replace with bytemuck::cast_slice + to_vec(), which safely casts the byte slice and copies into a properly-aligned Vec<T>. The Pod bound already guarantees valid bit patterns for any byte sequence. Closes FyroxEngine#827
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.
Summary
Vec::from_raw_parts+mem::forgetwith safebytemuck::cast_slice+to_vec()mem::forgetreceived aVec<u8>whose memory was already transferred to anotherVecVec<T>would deallocate withalign_of::<T>()but the memory was allocated withalign_of::<u8>() = 1Vec<T>bytemuckis already a dependency offyrox-graphicsTest plan
cargo check -p fyrox-graphicspassesCloses #827