audit use of to_cstring and fix ub

This commit is contained in:
dignifiedquire
2019-07-18 22:47:47 +02:00
committed by holger krekel
parent 618087e5a7
commit c68e7ae14e
26 changed files with 265 additions and 247 deletions

View File

@@ -111,7 +111,7 @@ pub unsafe extern "C" fn dc_get_config(
match config::Config::from_str(dc_tools::as_str(key)) {
Ok(key) => {
let value = context.get_config(key).unwrap_or_default();
into_cstring(value)
dc_tools::to_cstring(value)
}
Err(_) => std::ptr::null_mut(),
}
@@ -137,7 +137,7 @@ pub unsafe extern "C" fn dc_get_oauth2_url(
let addr = dc_tools::to_string(addr);
let redirect = dc_tools::to_string(redirect);
match oauth2::dc_get_oauth2_url(context, addr, redirect) {
Some(res) => libc::strdup(dc_tools::to_cstring(res).as_ptr()),
Some(res) => dc_tools::to_cstring(res),
None => std::ptr::null_mut(),
}
}
@@ -1546,7 +1546,3 @@ fn as_opt_str<'a>(s: *const libc::c_char) -> Option<&'a str> {
Some(dc_tools::as_str(s))
}
unsafe fn into_cstring(s: impl AsRef<str>) -> *mut libc::c_char {
dc_tools::dc_strdup(dc_tools::to_cstring(s).as_ptr())
}