Skip to content

SDL_RendererFlip invalid enum variant #1504

@jagprog5

Description

@jagprog5

this panics when compiled in non release mode because the unsafe creates an invalid enum variant. 1 | 2 = 3.

It didn't do this before - maybe new assertions in the latest compiler:

john@DESKTOP-SJV22UD:~/game_system$ rustc --version
rustc 1.90.0 (1159e78c4 2025-09-14)
#[repr(u32)]
#[doc = " Flip constants for SDL_RenderCopyEx"]
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub enum SDL_RendererFlip {
    #[doc = "< Do not flip"]
    SDL_FLIP_NONE = 0,
    #[doc = "< flip horizontally"]
    SDL_FLIP_HORIZONTAL = 1,
    #[doc = "< flip vertically"]
    SDL_FLIP_VERTICAL = 2,
}

let flip: 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>( // ALWAYS BAD
            transmute::<sys::SDL_RendererFlip, u32>(SDL_FLIP_HORIZONTAL)
                | transmute::<sys::SDL_RendererFlip, u32>(SDL_FLIP_VERTICAL),
        ),
    }
};
john@DESKTOP-SJV22UD:~/game_system$ rustc --version
rustc 1.90.0 (1159e78c4 2025-09-14)

thread 'main' panicked at /home/john/.cargo/git/checkouts/rust-sdl2-4b90a0874f1ccadd/52de591/src/sdl2/render.rs:1752:33:
trying to construct an enum from an invalid value 0x3
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread caused non-unwinding panic. aborting.

Thread 1 "ui_6_button" received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff7830859 in __GI_abort () at abort.c:79
#2  0x000055555561e93a in std::sys::pal::unix::abort_internal () at library/std/src/sys/pal/unix/mod.rs:366
#3  0x000055555561c17d in std::panicking::rust_panic_with_hook () at library/std/src/panicking.rs:44
#4  0x000055555561bdaa in std::panicking::begin_panic_handler::{closure#0} () at library/std/src/panicking.rs:706
#5  0x000055555561ac59 in std::sys::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::{closure_env#0}, !> () at library/std/src/sys/backtrace.rs:174
#6  0x000055555561ba3d in std::panicking::begin_panic_handler () at library/std/src/panicking.rs:697
#7  0x0000555555635c6d in core::panicking::panic_nounwind_fmt::runtime () at library/core/src/panicking.rs:117
#8  core::panicking::panic_nounwind_fmt () at library/core/src/intrinsics/mod.rs:2367
#9  0x0000555555635ea6 in core::panicking::panic_invalid_enum_construction () at library/core/src/panicking.rs:327
#10 0x000055555559f055 in sdl2::render::Canvas<sdl2::video::Window>::copy_ex<sdl2::video::Window, core::option::Option<sdl2::rect::Rect>, sdl2::rect::Rect, core::option::Option<sdl2::rect::Point>>
    (self=0x7fffffffc260, texture=0x555556409de8, src=..., dst=..., angle=0, center=..., flip_horizontal=true, flip_vertical=true) at /home/john/.cargo/git/checkouts/rust-sdl2-4b90a0874f1ccadd/52de591/src/sdl2/render.rs:1752
#11 0x00005555555abe9c in game_system::core::backends::rust_sdl2::{impl#4}::copy<game_system::core::texture_rect::TextureRect, game_system::core::texture_rect::TextureDestination> (self=0x7fffffffad70, src=..., dst=...) at src/core/backends/rust_sdl2/mod.rs:182
#12 0x00005555555b065c in game_system::ui::widget::border::{impl#1}::draw<game_system::core::backends::rust_sdl2::RustSDL2System> (self=0x5555563c0b70, sys_interface=0x7fffffffc220) at src/ui/widget/border.rs:522
#13 0x00005555555a1e94 in game_system::ui::widget::vertical_layout::{impl#1}::draw<game_system::core::backends::rust_sdl2::RustSDL2System> (self=0x555556360250, sys_interface=0x7fffffffc220) at src/ui/widget/vertical_layout.rs:348
#14 0x00005555555a66cd in game_system::ui::widget::background::{impl#1}::draw<game_system::core::backends::rust_sdl2::RustSDL2System> (self=0x7fffffffd338, sys_interface=0x7fffffffc220) at src/ui/widget/background.rs:126
#15 0x00005555555b424a in ui_6_button::do_example::{closure#1}<game_system::core::backends::rust_sdl2::RustSDL2System> (system=0x7fffffffc220, events=..., dt=...) at examples/ui_6_button.rs:195
#16 0x00005555555994aa in game_system::ui::widget::gui_loop<game_system::core::backends::rust_sdl2::RustSDL2System, ui_6_button::do_example::{closure_env#1}<game_system::core::backends::rust_sdl2::RustSDL2System>> (max_delay=..., system_interface=0x7fffffffc220, handler=...)
    at src/ui/widget/mod.rs:354
#17 0x00005555555b3a03 in ui_6_button::do_example<game_system::core::backends::rust_sdl2::RustSDL2System> (font_file_content=...) at examples/ui_6_button.rs:152
#18 0x00005555555a9029 in ui_6_button::main () at examples/ui_6_button.rs:207

in sdl3-sys, the representation is instead:

#[repr(transparent)]
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct SDL_FlipMode(pub ::core::ffi::c_int);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions