fix: mark as_path() function unsafe

This commit is contained in:
link2xt
2026-07-27 23:48:11 +00:00
committed by l
parent 2d7af15124
commit 733bec326a
2 changed files with 17 additions and 11 deletions
+5 -5
View File
@@ -101,7 +101,7 @@ pub unsafe extern "C" fn dc_context_new(
// generate random ID as this functionality is not yet available on the C-api.
let id = rand::random();
block_on(
ContextBuilder::new(as_path(dbfile).to_path_buf())
ContextBuilder::new(unsafe { as_path(dbfile) }.to_path_buf())
.with_id(id)
.open(),
)
@@ -129,7 +129,7 @@ pub unsafe extern "C" fn dc_context_new_closed(dbfile: *const libc::c_char) -> *
let id = rand::random();
match block_on(
ContextBuilder::new(as_path(dbfile).to_path_buf())
ContextBuilder::new(unsafe { as_path(dbfile) }.to_path_buf())
.with_id(id)
.build(),
) {
@@ -3728,7 +3728,7 @@ pub unsafe extern "C" fn dc_msg_set_file_and_deduplicate(
.message
.set_file_and_deduplicate(
ctx,
as_path(file),
unsafe { as_path(file) },
to_opt_string_lossy(name).as_deref(),
to_opt_string_lossy(filemime).as_deref(),
)
@@ -4448,7 +4448,7 @@ pub unsafe extern "C" fn dc_accounts_new(
return ptr::null_mut();
}
let accs = block_on(Accounts::new(as_path(dir).into(), writable != 0));
let accs = block_on(Accounts::new(unsafe { as_path(dir) }.into(), writable != 0));
match accs {
Ok(accs) => Arc::into_raw(Arc::new(RwLock::new(accs))),
@@ -4542,7 +4542,7 @@ pub unsafe extern "C" fn dc_accounts_new_with_event_channel(
};
let accs = block_on(Accounts::new_with_events(
as_path(dir).into(),
unsafe { as_path(dir) }.into(),
writable != 0,
event_channel,
));
+12 -6
View File
@@ -263,7 +263,7 @@ pub(crate) fn to_opt_string_lossy(s: *const libc::c_char) -> Option<String> {
///
/// [Path]: std::path::Path
#[cfg(not(target_os = "windows"))]
pub(crate) fn as_path<'a>(s: *const libc::c_char) -> &'a std::path::Path {
pub(crate) unsafe fn as_path<'a>(s: *const libc::c_char) -> &'a std::path::Path {
assert!(!s.is_null(), "cannot be used on null pointers");
use std::os::unix::ffi::OsStrExt;
let c_str = unsafe { std::ffi::CStr::from_ptr(s) }.to_bytes();
@@ -273,8 +273,8 @@ pub(crate) fn as_path<'a>(s: *const libc::c_char) -> &'a std::path::Path {
// as_path() implementation for windows, documented above.
#[cfg(target_os = "windows")]
pub(crate) fn as_path<'a>(s: *const libc::c_char) -> &'a std::path::Path {
as_path_unicode(s)
pub(crate) unsafe fn as_path<'a>(s: *const libc::c_char) -> &'a std::path::Path {
unsafe { as_path_unicode(s) }
}
// Implementation for as_path() on Windows.
@@ -282,7 +282,7 @@ pub(crate) fn as_path<'a>(s: *const libc::c_char) -> &'a std::path::Path {
// Having this as a separate function means it can be tested on unix
// too.
#[allow(dead_code)]
fn as_path_unicode<'a>(s: *const libc::c_char) -> &'a std::path::Path {
unsafe fn as_path_unicode<'a>(s: *const libc::c_char) -> &'a std::path::Path {
assert!(!s.is_null(), "cannot be used on null pointers");
let cstr = unsafe { CStr::from_ptr(s) };
@@ -370,14 +370,20 @@ mod tests {
fn test_as_path() {
let some_path = CString::new("/some/path").unwrap();
let ptr = some_path.as_ptr();
assert_eq!(as_path(ptr), std::ffi::OsString::from("/some/path"))
assert_eq!(
unsafe { as_path(ptr) },
std::ffi::OsString::from("/some/path")
)
}
#[test]
fn test_as_path_unicode_fn() {
let some_path = CString::new("/some/path").unwrap();
let ptr = some_path.as_ptr();
assert_eq!(as_path_unicode(ptr), std::ffi::OsString::from("/some/path"));
assert_eq!(
unsafe { as_path_unicode(ptr) },
std::ffi::OsString::from("/some/path")
);
}
#[test]