diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index d430c798f..c43675444 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -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, )); diff --git a/deltachat-ffi/src/string.rs b/deltachat-ffi/src/string.rs index 6041121ef..4fac1b06d 100644 --- a/deltachat-ffi/src/string.rs +++ b/deltachat-ffi/src/string.rs @@ -263,7 +263,7 @@ pub(crate) fn to_opt_string_lossy(s: *const libc::c_char) -> Option { /// /// [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]