Skip to content

Commit

Permalink
Force consistent use of unsafe blocks
Browse files Browse the repository at this point in the history
Isolating unsafe code is still important even in unsafe functions
  • Loading branch information
FenrirWolf committed Feb 15, 2024
1 parent e593abd commit 3340fae
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions ctru-rs/src/applets/swkbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ impl SoftwareKeyboard {
// an intermediate fixed-size buffer
//
// 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 {
use ctru_sys::{
MEMPERM_READ, MEMPERM_WRITE, R_FAILED, SWKBD_BUTTON_LEFT, SWKBD_BUTTON_MIDDLE,
Expand Down Expand Up @@ -831,15 +832,15 @@ impl SoftwareKeyboard {
// 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
// simply reuse their version
#[allow(non_snake_case)]
#[deny(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn swkbd_message_callback(
user: *mut libc::c_void,
sender: NS_APPID,
msg: *mut libc::c_void,
msg_size: libc::size_t,
) {
let extra = &mut *user.cast::<SwkbdExtra>();
let swkbd = &mut *msg.cast::<SwkbdState>();
let extra = unsafe { &mut *user.cast::<SwkbdExtra>() };
let swkbd = unsafe { &mut *msg.cast::<SwkbdState>() };

if sender != ctru_sys::APPID_SOFTWARE_KEYBOARD
|| msg_size != std::mem::size_of::<SwkbdState>()
Expand All @@ -859,12 +860,14 @@ impl SoftwareKeyboard {
let mut retmsg = std::ptr::null();

if let Some(cb) = extra.callback {
swkbd.callback_result = cb(
extra.callback_user,
&mut retmsg,
text8.as_ptr(),
text8.len(),
) as _
swkbd.callback_result = unsafe {
cb(
extra.callback_user,
&mut retmsg,
text8.as_ptr(),
text8.len(),
)
} as _
};

let retmsg = if !retmsg.is_null() {
Expand All @@ -886,14 +889,16 @@ impl SoftwareKeyboard {
callback_msg[idx] = code_point;
}

let _ = APT_SendParameter(
envGetAptAppId(),
sender,
APTCMD_MESSAGE,
swkbd as *mut _ as *mut _,
std::mem::size_of::<SwkbdState>() as _,
0,
);
let _ = unsafe {
APT_SendParameter(
envGetAptAppId(),
sender,
APTCMD_MESSAGE,
swkbd as *mut _ as *mut _,
std::mem::size_of::<SwkbdState>() as _,
0,
)
};
}
}

Expand Down

0 comments on commit 3340fae

Please sign in to comment.