Skip to content

Invalid u32 transmute to a SDL_RendererFlip causing a panic in 0.38.0 crate version #1528

Description

@ThingWithBeret

When you attempt to render a texture with the canvas.copy_ex_f() function where both flip_{horizontal/vertical} are true, an invalid enum variant of SDL_RenderFlip is created via a transmute, which causes a panic. The message being:

thread 'main' (12499107) panicked at user/.cargo/registry/src/index.crates.io-*/sdl2-0.38.0/src/sdl2/render.rs:1646:33:
trying to construct an enum from an invalid value 0x3
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This is this panic-causing operation located in line 1640 of src/sdl2.render.rs:

use crate::sys::SDL_RendererFlip::*;
let flip: sys::SDL_RendererFlip = unsafe {
    match (flip_horizontal, flip_vertical) {
        (false, false) => SDL_FLIP_NONE,
        (true, false) => SDL_FLIP_HORIZONTAL,
        (false, true) => SDL_FLIP_VERTICAL,
        (true, true) => transmute::<u32, sys::SDL_RendererFlip>(
            transmute::<sys::SDL_RendererFlip, u32>(SDL_FLIP_HORIZONTAL)
                | transmute::<sys::SDL_RendererFlip, u32>(SDL_FLIP_VERTICAL),
        ),
    }
};

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions