Skip to content

Commit

Permalink
Merge pull request #165 from FenrirWolf/unsafe_blocks_in_unsafe_fn
Browse files Browse the repository at this point in the history
Require unsafe blocks in unsafe fns
  • Loading branch information
FenrirWolf authored Feb 23, 2024
2 parents 0345f49 + a18dfc4 commit 10163fd
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
1 change: 0 additions & 1 deletion ctru-rs/src/applets/swkbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,6 @@ impl SoftwareKeyboard {

// A reimplementation of `swkbdMessageCallback` from `libctru/source/applets/swkbd.c`.
// This function sets up and then calls the callback set by `swkbdSetFilterCallback`
#[deny(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn swkbd_message_callback(
user: *mut libc::c_void,
sender: NS_APPID,
Expand Down
1 change: 0 additions & 1 deletion ctru-rs/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//! Have a look at [`Soc::redirect_to_3dslink()`](crate::services::soc::Soc::redirect_to_3dslink) for a better alternative when debugging applications.

use std::cell::{RefMut, UnsafeCell};
use std::default::Default;

use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole};

Expand Down
1 change: 1 addition & 0 deletions ctru-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#![crate_type = "rlib"]
#![crate_name = "ctru"]
#![warn(missing_docs)]
#![deny(unsafe_op_in_unsafe_fn)]
#![feature(custom_test_frameworks)]
#![feature(try_trait_v2)]
#![feature(allocator_api)]
Expand Down
4 changes: 3 additions & 1 deletion ctru-rs/src/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ unsafe impl Allocator for LinearAllocator {

#[doc(alias = "linearFree")]
unsafe fn deallocate(&self, ptr: NonNull<u8>, _layout: Layout) {
ctru_sys::linearFree(ptr.as_ptr().cast());
unsafe {
ctru_sys::linearFree(ptr.as_ptr().cast());
}
}
}
8 changes: 5 additions & 3 deletions ctru-rs/src/services/ir_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,11 @@ impl IrUser {
let mut shared_mem_guard = IR_USER_STATE.lock().unwrap();
let shared_mem = shared_mem_guard.as_mut().unwrap();

shared_mem
.service_handle
.send_service_request(request, expected_response_len)
unsafe {
shared_mem
.service_handle
.send_service_request(request, expected_response_len)
}
}
}

Expand Down
1 change: 0 additions & 1 deletion ctru-rs/src/services/ndsp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::error::ResultCode;
use crate::services::ServiceReference;

use std::cell::{RefCell, RefMut};
use std::default::Default;
use std::error;
use std::fmt;
use std::sync::Mutex;
Expand Down
23 changes: 16 additions & 7 deletions ctru-rs/src/services/svc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,28 @@ impl HandleExt for Handle {
) -> crate::Result<Vec<u32>> {
// Copy over the request
let cmd_buffer_ptr = unsafe { ctru_sys::getThreadCommandBuffer() };
std::ptr::copy_nonoverlapping(request.as_ptr(), cmd_buffer_ptr, request.len());

// Send the request
ResultCode(ctru_sys::svcSendSyncRequest(self))?;
unsafe {
std::ptr::copy_nonoverlapping(request.as_ptr(), cmd_buffer_ptr, request.len());

// Send the request
ResultCode(ctru_sys::svcSendSyncRequest(self))?;

// Handle the result returned by the service
let result = unsafe { std::ptr::read(cmd_buffer_ptr.add(1)) };
ResultCode(result as ctru_sys::Result)?;
// Handle the result returned by the service
let result = std::ptr::read(cmd_buffer_ptr.add(1));
ResultCode(result as ctru_sys::Result)?;
}

// Copy back the response
request.clear();
request.resize(expected_response_len, 0);
std::ptr::copy_nonoverlapping(cmd_buffer_ptr, request.as_mut_ptr(), expected_response_len);
unsafe {
std::ptr::copy_nonoverlapping(
cmd_buffer_ptr,
request.as_mut_ptr(),
expected_response_len,
);
}

Ok(request)
}
Expand Down

0 comments on commit 10163fd

Please sign in to comment.