Skip to content

Unsound Pod implementation in released crate (not on main) #160

Description

@Manishearth

https://docs.rs/rgb/0.8.53/rgb/struct.Rgba.html#impl-Pod-for-Rgba%3CT,+A%3E

This impl is unsound: if T != A there can be padding bytes.

This is fixed on main, though.

unsafe impl<T: ::bytemuck::Zeroable> ::bytemuck::Zeroable for $name<T> {}
unsafe impl<T: ::bytemuck::Pod> ::bytemuck::Pod for $name<T> {}

Miri repro:

use rgb::RGBA;

fn main() {
    let pixel = RGBA { r: 1u8, g: 2u8, b: 3u8, a: 4u16 };
    let bytes = bytemuck::bytes_of(&pixel);
    let _ub = bytes[3];
}
error: Undefined Behavior: reading memory at alloc107[0x3..0x4], but memory is uninitialized at [0x3..0x4], and this operation requires initialized memory
 --> src/bin/repro1.rs:6:15
  |
6 |     let _ub = bytes[3];
  |               ^^^^^^^^ Undefined Behavior occurred here
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

Uninitialized memory occurred at alloc107[0x3..0x4], in this allocation:
alloc107 (stack variable, size: 6, align: 2) {
    01 02 03 __ 04 00                               │ ...░..
}

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions