Skip to content

Commit

Permalink
fix ub
Browse files Browse the repository at this point in the history
  • Loading branch information
antonilol committed Sep 18, 2024
1 parent 400e033 commit 5fe94bd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 39 deletions.
13 changes: 5 additions & 8 deletions src/sdl2/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,11 @@ impl TryFrom<u32> for AudioStatus {
type Error = ();

fn try_from(n: u32) -> Result<Self, Self::Error> {
use self::AudioStatus::*;
use crate::sys::SDL_AudioStatus::*;

Ok(match unsafe { mem::transmute(n) } {
SDL_AUDIO_STOPPED => Stopped,
SDL_AUDIO_PLAYING => Playing,
SDL_AUDIO_PAUSED => Paused,
})
if n <= 2 {
Ok(unsafe { mem::transmute::<u32, Self>(n) })
} else {
Err(())
}
}
}

Expand Down
48 changes: 20 additions & 28 deletions src/sdl2/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::get_error;
use libc::c_char;
use libc::c_void;
use std::error;
use std::ffi::{CStr, CString, NulError};
Expand All @@ -9,17 +8,15 @@ use crate::sys;

#[doc(alias = "SDL_GetBasePath")]
pub fn base_path() -> Result<String, String> {
let result = unsafe {
unsafe {
let buf = sys::SDL_GetBasePath();
let s = CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned();
sys::SDL_free(buf as *mut c_void);
s
};

if result.is_empty() {
Err(get_error())
} else {
Ok(result)
if buf.is_null() {
Err(get_error())
} else {
let s = CStr::from_ptr(buf).to_str().unwrap().to_owned();
sys::SDL_free(buf as *mut c_void);
Ok(s)
}
}
}

Expand Down Expand Up @@ -58,23 +55,18 @@ impl error::Error for PrefPathError {
#[doc(alias = "SDL_GetPrefPath")]
pub fn pref_path(org_name: &str, app_name: &str) -> Result<String, PrefPathError> {
use self::PrefPathError::*;
let result = unsafe {
let org = match CString::new(org_name) {
Ok(s) => s,
Err(err) => return Err(InvalidOrganizationName(err)),
};
let app = match CString::new(app_name) {
Ok(s) => s,
Err(err) => return Err(InvalidApplicationName(err)),
};
let buf =
sys::SDL_GetPrefPath(org.as_ptr() as *const c_char, app.as_ptr() as *const c_char);
CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned()
};

if result.is_empty() {
Err(SdlError(get_error()))
} else {
Ok(result)
let org = CString::new(org_name).map_err(InvalidOrganizationName)?;
let app = CString::new(app_name).map_err(InvalidApplicationName)?;

unsafe {
let buf = sys::SDL_GetPrefPath(org.as_ptr(), app.as_ptr());
if buf.is_null() {
Err(SdlError(get_error()))
} else {
let ret = CStr::from_ptr(buf).to_str().unwrap().to_owned();
sys::SDL_free(buf as *mut c_void);
Ok(ret)
}
}
}
4 changes: 2 additions & 2 deletions src/sdl2/mixer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ pub fn get_chunk_decoder(index: i32) -> String {
/// The internal format for an audio chunk.
#[derive(PartialEq)]
pub struct Chunk {
pub raw: *mut mixer::Mix_Chunk,
pub owned: bool,
raw: *mut mixer::Mix_Chunk,
owned: bool,
}

impl Drop for Chunk {
Expand Down
8 changes: 7 additions & 1 deletion src/sdl2/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,9 @@ impl std::iter::Sum for Point {
/// recommended to use `Option<FRect>`, with `None` representing an empty
/// rectangle (see, for example, the output of the
/// [`intersection`](#method.intersection) method).
// Uses repr(transparent) to allow pointer casting between FRect and SDL_FRect (see
// `FRect::raw_slice`)
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct FRect {
raw: sys::SDL_FRect,
Expand Down Expand Up @@ -1357,7 +1360,7 @@ impl FRect {
}

pub fn raw_mut(&mut self) -> *mut sys::SDL_FRect {
self.raw() as *mut _
&mut self.raw
}

#[doc(alias = "SDL_FRect")]
Expand Down Expand Up @@ -1600,6 +1603,9 @@ impl BitOr<FRect> for FRect {
}

/// Immutable point type with float precision, consisting of x and y.
// Uses repr(transparent) to allow pointer casting between FPoint and SDL_FPoint (see
// `FPoint::raw_slice`)
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct FPoint {
raw: sys::SDL_FPoint,
Expand Down

0 comments on commit 5fe94bd

Please sign in to comment.