Skip to content

Commit a0734fd

Browse files
committed
Fix undefined behavior with AudioStatus's FromPrimitive
Transmuting from a value not in the enum is undefined, plus transmuting is generally not great to do.
1 parent 0ca6469 commit a0734fd

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/sdl2/audio.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ use std::ops::{Deref, DerefMut};
6060
use std::path::Path;
6161
use std::marker::PhantomData;
6262
use std::mem;
63-
use std::mem::transmute;
6463
use std::ptr;
6564

6665
use AudioSubsystem;
@@ -209,12 +208,16 @@ pub enum AudioStatus {
209208
impl FromPrimitive for AudioStatus {
210209
fn from_i64(n: i64) -> Option<AudioStatus> {
211210
use self::AudioStatus::*;
212-
let n = n as u32;
213211

214-
Some( match unsafe { transmute::<u32, sys::SDL_AudioStatus>(n) } {
215-
sys::SDL_AudioStatus::SDL_AUDIO_STOPPED => Stopped,
216-
sys::SDL_AudioStatus::SDL_AUDIO_PLAYING => Playing,
217-
sys::SDL_AudioStatus::SDL_AUDIO_PAUSED => Paused,
212+
const STOPPED: i64 = sys::SDL_AudioStatus::SDL_AUDIO_STOPPED as i64;
213+
const PLAYING: i64 = sys::SDL_AudioStatus::SDL_AUDIO_PLAYING as i64;
214+
const PAUSED: i64 = sys::SDL_AudioStatus::SDL_AUDIO_PAUSED as i64;
215+
216+
Some(match n {
217+
STOPPED => Stopped,
218+
PLAYING => Playing,
219+
PAUSED => Paused,
220+
_ => return None,
218221
})
219222
}
220223

0 commit comments

Comments
 (0)