mirror of
https://github.com/gopher64/gopher64.git
synced 2026-07-11 01:25:20 +02:00
protect against null ptr dereference (#819)
* protect against null ptr dereference * more
This commit is contained in:
@@ -99,13 +99,15 @@ impl Drop for Ui {
|
||||
pub fn sdl_init(flag: sdl3_sys::init::SDL_InitFlags) {
|
||||
unsafe {
|
||||
if sdl3_sys::init::SDL_WasInit(flag) == 0 && !sdl3_sys::init::SDL_InitSubSystem(flag) {
|
||||
let err = std::ffi::CStr::from_ptr(sdl3_sys::error::SDL_GetError())
|
||||
.to_str()
|
||||
.unwrap();
|
||||
let err = sdl3_sys::error::SDL_GetError();
|
||||
panic!(
|
||||
"Could not initialize SDL subsystem: {}, {}",
|
||||
u32::from(flag),
|
||||
err
|
||||
if err.is_null() {
|
||||
"Unknown error"
|
||||
} else {
|
||||
std::ffi::CStr::from_ptr(err).to_str().unwrap()
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+20
-24
@@ -332,10 +332,12 @@ pub fn get_controller_names(game_ui: &ui::Ui) -> Vec<String> {
|
||||
let mut controllers: Vec<String> = vec![];
|
||||
|
||||
for joystick in game_ui.input.joysticks.iter() {
|
||||
let name = unsafe {
|
||||
std::ffi::CStr::from_ptr(sdl3_sys::joystick::SDL_GetJoystickNameForID(*joystick))
|
||||
};
|
||||
controllers.push(name.to_string_lossy().to_string());
|
||||
let name = unsafe { sdl3_sys::joystick::SDL_GetJoystickNameForID(*joystick) };
|
||||
controllers.push(if name.is_null() {
|
||||
"Unknown controller".to_string()
|
||||
} else {
|
||||
unsafe { std::ffi::CStr::from_ptr(name).to_str().unwrap() }.to_string()
|
||||
});
|
||||
}
|
||||
|
||||
controllers
|
||||
@@ -346,12 +348,12 @@ pub fn get_controller_paths(game_ui: &ui::Ui) -> Vec<Option<String>> {
|
||||
let mut controller_paths: Vec<Option<String>> = vec![];
|
||||
|
||||
for joystick in game_ui.input.joysticks.iter() {
|
||||
let path = unsafe {
|
||||
std::ffi::CStr::from_ptr(sdl3_sys::joystick::SDL_GetJoystickPathForID(*joystick))
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
};
|
||||
controller_paths.push(Some(path));
|
||||
let path = unsafe { sdl3_sys::joystick::SDL_GetJoystickPathForID(*joystick) };
|
||||
controller_paths.push(if path.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }.to_string())
|
||||
});
|
||||
}
|
||||
|
||||
controller_paths
|
||||
@@ -470,13 +472,12 @@ pub fn get(ui: &mut ui::Ui, channel: usize) -> InputData {
|
||||
pub fn assign_controller(ui: &mut ui::Ui, controller: i32, port: usize) {
|
||||
if controller < ui.input.joysticks.len() as i32 {
|
||||
let path = unsafe {
|
||||
std::ffi::CStr::from_ptr(sdl3_sys::joystick::SDL_GetJoystickPathForID(
|
||||
ui.input.joysticks[controller as usize],
|
||||
))
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
sdl3_sys::joystick::SDL_GetJoystickPathForID(ui.input.joysticks[controller as usize])
|
||||
};
|
||||
ui.config.input.controller_assignment[port - 1] = Some(path);
|
||||
if !path.is_null() {
|
||||
ui.config.input.controller_assignment[port - 1] =
|
||||
Some(unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap().to_string() });
|
||||
}
|
||||
} else {
|
||||
println!("Invalid controller number")
|
||||
}
|
||||
@@ -977,14 +978,9 @@ pub fn init(ui: &mut ui::Ui) {
|
||||
let assigned_path = controller_assignment.as_ref().unwrap();
|
||||
|
||||
for joystick in ui.input.joysticks.iter() {
|
||||
let path = unsafe {
|
||||
std::ffi::CStr::from_ptr(sdl3_sys::joystick::SDL_GetJoystickPathForID(
|
||||
*joystick,
|
||||
))
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
};
|
||||
if path == *assigned_path
|
||||
let path = unsafe { sdl3_sys::joystick::SDL_GetJoystickPathForID(*joystick) };
|
||||
if !path.is_null()
|
||||
&& unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() } == *assigned_path
|
||||
&& unsafe { sdl3_sys::joystick::SDL_GetJoystickFromID(*joystick) }.is_null()
|
||||
&& unsafe { sdl3_sys::gamepad::SDL_GetGamepadFromID(*joystick) }.is_null()
|
||||
{
|
||||
|
||||
+18
-10
@@ -73,18 +73,26 @@ pub fn init(device: &mut device::Device) {
|
||||
sdl3_sys::video::SDL_CreateWindow(window_title.as_ptr(), window_width, window_height, flags)
|
||||
};
|
||||
if device.ui.video.window.is_null() {
|
||||
panic!("Could not create window: {}", unsafe {
|
||||
std::ffi::CStr::from_ptr(sdl3_sys::error::SDL_GetError())
|
||||
.to_str()
|
||||
.unwrap()
|
||||
});
|
||||
let err = sdl3_sys::error::SDL_GetError();
|
||||
panic!(
|
||||
"Could not create window: {}",
|
||||
if err.is_null() {
|
||||
"Unknown error"
|
||||
} else {
|
||||
unsafe { std::ffi::CStr::from_ptr(err).to_str().unwrap() }
|
||||
}
|
||||
);
|
||||
}
|
||||
if !unsafe { sdl3_sys::video::SDL_ShowWindow(device.ui.video.window) } {
|
||||
panic!("Could not show window: {}", unsafe {
|
||||
std::ffi::CStr::from_ptr(sdl3_sys::error::SDL_GetError())
|
||||
.to_str()
|
||||
.unwrap()
|
||||
});
|
||||
let err = sdl3_sys::error::SDL_GetError();
|
||||
panic!(
|
||||
"Could not show window: {}",
|
||||
if err.is_null() {
|
||||
"Unknown error"
|
||||
} else {
|
||||
unsafe { std::ffi::CStr::from_ptr(err).to_str().unwrap() }
|
||||
}
|
||||
);
|
||||
}
|
||||
unsafe {
|
||||
sdl3_sys::everything::SDL_HideCursor();
|
||||
|
||||
Reference in New Issue
Block a user