You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! I was trying to get some of the basic drawing functions working for an SDL 1.2 library and I was looking here for sample code. However, I was having issues with some colors not working. After investigating I found that ToColor uses UB because the layout of tuples in memory isn't guaranteed (so mem::transmute won't always work).
I think most uses of transmute can be replaced with either u32::to_be_bytes or u32::from_be_bytes to fix the issue. Ideally, there could be a single color type for the whole crate, but that might be too complex.
Here's a sample test which fails for me:
#[test]
fn test_color() {
let r: u8 = 255;
let g: u8 = 0;
let b: u8 = 255;
let a: u8 = 255;
assert_eq!((r, g, b, a), 0xFF00FFFFu32.as_rgba());
}
It's not a great test, but it shows the issue I'm running into.
The text was updated successfully, but these errors were encountered:
belak
changed the title
Implementation of sdl2::gfx::primitives::ToColor has undefined behavior
Implementations of sdl2::gfx::primitives::ToColor has undefined behavior
Apr 30, 2023
https://github.com/Rust-SDL2/rust-sdl2/blob/7fd7ee4/src/sdl2/gfx/primitives.rs#L21
Hello! I was trying to get some of the basic drawing functions working for an SDL 1.2 library and I was looking here for sample code. However, I was having issues with some colors not working. After investigating I found that ToColor uses UB because the layout of tuples in memory isn't guaranteed (so mem::transmute won't always work).
I think most uses of transmute can be replaced with either u32::to_be_bytes or u32::from_be_bytes to fix the issue. Ideally, there could be a single color type for the whole crate, but that might be too complex.
Here's a sample test which fails for me:
It's not a great test, but it shows the issue I'm running into.
The text was updated successfully, but these errors were encountered: