Browse Source

Force consistent use of unsafe blocks

Isolating unsafe code is still important even in unsafe functions
pull/157/head
Fenrir 10 months ago
parent
commit
bf3ca2a87d
  1. 19
      ctru-rs/src/applets/swkbd.rs

19
ctru-rs/src/applets/swkbd.rs

@ -633,6 +633,7 @@ impl SoftwareKeyboard {
// an intermediate fixed-size buffer // an intermediate fixed-size buffer
// //
// SAFETY: `swkbd` must be initialized by `swkbdInit` before calling this function. // SAFETY: `swkbd` must be initialized by `swkbdInit` before calling this function.
#[deny(unsafe_op_in_unsafe_fn)]
unsafe fn swkbd_input_text(swkbd: &mut SwkbdState, output: &mut String) -> SwkbdButton { unsafe fn swkbd_input_text(swkbd: &mut SwkbdState, output: &mut String) -> SwkbdButton {
use ctru_sys::{ use ctru_sys::{
MEMPERM_READ, MEMPERM_WRITE, R_FAILED, SWKBD_BUTTON_LEFT, SWKBD_BUTTON_MIDDLE, MEMPERM_READ, MEMPERM_WRITE, R_FAILED, SWKBD_BUTTON_LEFT, SWKBD_BUTTON_MIDDLE,
@ -842,15 +843,15 @@ impl SoftwareKeyboard {
// A reimplementation of `swkbdMessageCallback` from `libctru/source/applets/swkbd.c`. // A reimplementation of `swkbdMessageCallback` from `libctru/source/applets/swkbd.c`.
// This is only needed because the original function is private to libctru, so we can't // This is only needed because the original function is private to libctru, so we can't
// simply reuse their version // simply reuse their version
#[allow(non_snake_case)] #[deny(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn swkbd_message_callback( unsafe extern "C" fn swkbd_message_callback(
user: *mut libc::c_void, user: *mut libc::c_void,
sender: NS_APPID, sender: NS_APPID,
msg: *mut libc::c_void, msg: *mut libc::c_void,
msg_size: libc::size_t, msg_size: libc::size_t,
) { ) {
let extra = &mut *user.cast::<SwkbdExtra>(); let extra = unsafe { &mut *user.cast::<SwkbdExtra>() };
let swkbd = &mut *msg.cast::<SwkbdState>(); let swkbd = unsafe { &mut *msg.cast::<SwkbdState>() };
if sender != ctru_sys::APPID_SOFTWARE_KEYBOARD if sender != ctru_sys::APPID_SOFTWARE_KEYBOARD
|| msg_size != std::mem::size_of::<SwkbdState>() || msg_size != std::mem::size_of::<SwkbdState>()
@ -870,12 +871,14 @@ impl SoftwareKeyboard {
let mut retmsg = std::ptr::null(); let mut retmsg = std::ptr::null();
if let Some(cb) = extra.callback { if let Some(cb) = extra.callback {
swkbd.callback_result = cb( swkbd.callback_result = unsafe {
cb(
extra.callback_user, extra.callback_user,
&mut retmsg, &mut retmsg,
text8.as_ptr(), text8.as_ptr(),
text8.len(), text8.len(),
) as _ )
} as _
}; };
let retmsg = if !retmsg.is_null() { let retmsg = if !retmsg.is_null() {
@ -897,14 +900,16 @@ impl SoftwareKeyboard {
callback_msg[idx] = code_point; callback_msg[idx] = code_point;
} }
let _ = APT_SendParameter( let _ = unsafe {
APT_SendParameter(
envGetAptAppId(), envGetAptAppId(),
sender, sender,
APTCMD_MESSAGE, APTCMD_MESSAGE,
swkbd as *mut _ as *mut _, swkbd as *mut _ as *mut _,
std::mem::size_of::<SwkbdState>() as _, std::mem::size_of::<SwkbdState>() as _,
0, 0,
); )
};
} }
} }

Loading…
Cancel
Save