rust/ffi: Convert str func to new "view" naming convention

More obvious one shouldn't leak the pointer.

Closes: #1688
Approved by: jlebon
This commit is contained in:
Colin Walters 2018-11-26 16:37:28 +00:00 committed by Atomic Bot
parent db0319beac
commit bfe637a877
4 changed files with 7 additions and 7 deletions

View File

@ -37,12 +37,12 @@ use openat;
/// Convert a C (UTF-8) string to a &str; will panic
/// if it isn't valid UTF-8. Note the lifetime of
/// the return value must be <= the pointer.
pub fn str_from_nullable<'a>(s: *const libc::c_char) -> Option<&'a str> {
pub fn ffi_view_nullable_str<'a>(s: *const libc::c_char) -> Option<&'a str> {
if s.is_null() {
None
} else {
let s = unsafe { CStr::from_ptr(s) };
Some(s.to_str().unwrap())
Some(s.to_str().expect("ffi_view_nullable_str: valid utf-8"))
}
}

View File

@ -212,7 +212,7 @@ mod ffi {
#[no_mangle]
pub extern "C" fn ror_progress_set_sub_message(msg: *const libc::c_char) {
let msg = str_from_nullable(msg);
let msg = ffi_view_nullable_str(msg);
let mut lock = PROGRESS.lock().unwrap();
let state = lock.as_mut().expect("progress to update");
state.set_sub_message(msg);
@ -227,7 +227,7 @@ mod ffi {
#[no_mangle]
pub extern "C" fn ror_progress_end(suffix: *const libc::c_char) {
let suffix = str_from_nullable(suffix);
let suffix = ffi_view_nullable_str(suffix);
let mut lock = PROGRESS.lock().unwrap();
let state = lock.take().expect("progress to end");
state.end(suffix);

View File

@ -831,7 +831,7 @@ mod ffi {
) -> *mut Treefile {
// Convert arguments
let filename = OsStr::from_bytes(bytes_from_nonnull(filename));
let arch = str_from_nullable(arch);
let arch = ffi_view_nullable_str(arch);
let workdir = ffi_view_openat_dir(workdir_dfd);
// Run code, map error if any, otherwise extract raw pointer, passing
// ownership back to C.

View File

@ -128,7 +128,7 @@ mod ffi {
url: *const libc::c_char,
gerror: *mut *mut glib_sys::GError,
) -> libc::c_int {
let url = str_from_nullable(url).unwrap();
let url = ffi_view_nullable_str(url).unwrap();
match download_url_to_tmpfile(url) {
Ok(f) => f.into_raw_fd(),
Err(e) => {
@ -144,7 +144,7 @@ mod ffi {
h: *mut glib_sys::GHashTable,
gerror: *mut *mut glib_sys::GError,
) -> *mut libc::c_char {
let s = str_from_nullable(s).unwrap();
let s = ffi_view_nullable_str(s).unwrap();
let h_rs: HashMap<String, String> =
unsafe { glib::translate::FromGlibPtrContainer::from_glib_none(h) };
match varsubst(s, &h_rs) {