Skip to content

Fix read_pixels_of_type UB: use bytemuck::cast_slice#918

Open
kimjune01 wants to merge 1 commit into
FyroxEngine:masterfrom
kimjune01:fix/read-pixels-manually-drop
Open

Fix read_pixels_of_type UB: use bytemuck::cast_slice#918
kimjune01 wants to merge 1 commit into
FyroxEngine:masterfrom
kimjune01:fix/read-pixels-manually-drop

Conversation

@kimjune01
Copy link
Copy Markdown

Summary

  • Replaces unsafe Vec::from_raw_parts + mem::forget with safe bytemuck::cast_slice + to_vec()
  • The previous implementation had two sources of UB:
    1. mem::forget received a Vec<u8> whose memory was already transferred to another Vec
    2. Vec<T> would deallocate with align_of::<T>() but the memory was allocated with align_of::<u8>() = 1
  • The new implementation safely casts the byte slice and copies into a properly-aligned Vec<T>
  • bytemuck is already a dependency of fyrox-graphics

Test plan

  • cargo check -p fyrox-graphics passes
  • CI should verify no regressions across the full test suite

Closes #827

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GpuFrameBufferTrait::read_pixels_of_type contains UB

1 participant