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

@@ -71,7 +71,12 @@ impl Context {
Config::Selfavatar => {
let rel_path = self.sql.get_config(self, key);
rel_path.map(|p| {
let v = unsafe { dc_get_abs_path(self, to_cstring(p).as_ptr()) };
let v = unsafe {
let n = to_cstring(p);
let res = dc_get_abs_path(self, n);
free(n as *mut libc::c_void);
res
};
let r = to_string(v);
unsafe { free(v as *mut _) };
r

View File

@@ -279,7 +279,7 @@ unsafe fn cb_get_config(
.sql
.get_config(context, as_str(key))
.unwrap_or_else(|| to_string(def));
strdup(to_cstring(res).as_ptr())
to_cstring(res)
}
pub unsafe fn dc_context_unref(context: &mut Context) {
@@ -515,7 +515,7 @@ pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char {
fingerprint_str,
);
strdup(to_cstring(res).as_ptr())
to_cstring(res)
}
pub unsafe fn dc_get_version_str() -> *mut libc::c_char {

View File

@@ -374,7 +374,7 @@ pub unsafe fn dc_array_get_string(
}
res
});
strdup(to_cstring(res).as_ptr())
to_cstring(res)
}
/// return comma-separated value-string from integer array
@@ -396,7 +396,7 @@ pub unsafe fn dc_arr_to_string(arr: *const uint32_t, cnt: libc::c_int) -> *mut l
res
},
);
strdup(to_cstring(res).as_ptr())
to_cstring(res)
}
#[cfg(test)]

View File

@@ -130,15 +130,19 @@ pub fn dc_chat_load_from_db(chat: *mut Chat, chat_id: u32) -> bool {
c.type_0 = row.get(1)?;
c.name = {
let raw: String = row.get(2)?;
unsafe { strdup(to_cstring(raw).as_ptr()) }
unsafe { to_cstring(raw) }
};
c.grpid = {
let raw: String = row.get(3)?;
unsafe { strdup(to_cstring(raw).as_ptr()) }
unsafe { to_cstring(raw) }
};
let packed: String = row.get(4)?;
unsafe { dc_param_set_packed((*chat).param, to_cstring(&packed).as_ptr()) };
unsafe {
let p = to_cstring(&packed);
dc_param_set_packed((*chat).param, p);
free(p as *mut _);
};
c.archived = row.get(5)?;
c.blocked = row.get(6)?;
c.gossiped_timestamp = row.get(7)?;
@@ -496,8 +500,9 @@ unsafe fn prepare_msg_raw(
} else {
0 as *mut libc::c_char
},
from_c.as_ptr(),
from_c,
);
free(from_c as *mut _);
if (*chat).type_0 == 100 {
if let Some(id) = context.sql.query_row_col(
@@ -761,9 +766,9 @@ unsafe fn get_parent_mime_headers(
FROM msgs WHERE chat_id=? AND from_id!=?);",
params![(*chat).id as i32, 1],
|row| {
*parent_rfc724_mid = dc_strdup(to_cstring(row.get::<_, String>(0)?).as_ptr());
*parent_in_reply_to = dc_strdup(to_cstring(row.get::<_, String>(1)?).as_ptr());
*parent_references = dc_strdup(to_cstring(row.get::<_, String>(2)?).as_ptr());
*parent_rfc724_mid = to_cstring(row.get::<_, String>(0)?);
*parent_in_reply_to = to_cstring(row.get::<_, String>(1)?);
*parent_references = to_cstring(row.get::<_, String>(2)?);
Ok(())
},
)
@@ -779,12 +784,9 @@ unsafe fn get_parent_mime_headers(
FROM msgs WHERE chat_id=? AND from_id==?);",
params![(*chat).id as i32, 1],
|row| {
*parent_rfc724_mid =
dc_strdup(to_cstring(row.get::<_, String>(0)?).as_ptr());
*parent_in_reply_to =
dc_strdup(to_cstring(row.get::<_, String>(1)?).as_ptr());
*parent_references =
dc_strdup(to_cstring(row.get::<_, String>(2)?).as_ptr());
*parent_rfc724_mid = to_cstring(row.get::<_, String>(0)?);
*parent_in_reply_to = to_cstring(row.get::<_, String>(1)?);
*parent_references = to_cstring(row.get::<_, String>(2)?);
Ok(())
},
)
@@ -823,7 +825,8 @@ unsafe fn last_msg_in_chat_encrypted(
if let Some(packed) = packed {
let msg_param = dc_param_new();
let packed_c = to_cstring(packed);
dc_param_set_packed(msg_param, packed_c.as_ptr());
dc_param_set_packed(msg_param, packed_c);
free(packed_c as *mut _);
if 0 != dc_param_exists(msg_param, 'c' as i32) {
last_is_encrypted = 1;
@@ -2090,7 +2093,7 @@ pub unsafe fn dc_chat_get_subtitle(chat: *const Chat) -> *mut libc::c_char {
0,
)
.unwrap_or_else(|| "Err".into());
ret = dc_strdup(to_cstring(ret_raw).as_ptr());
ret = to_cstring(ret_raw);
} else if (*chat).type_0 == 120 || (*chat).type_0 == 130 {
if (*chat).id == 1 {
ret = dc_stock_str((*chat).context, 8)

View File

@@ -1098,12 +1098,14 @@ unsafe fn moz_autoconfigure(
tag_config: 0,
};
let xml_raw = read_autoconf_file(context, to_cstring(url).as_ptr());
let url_c = to_cstring(url);
let xml_raw = read_autoconf_file(context, url_c);
free(url_c as *mut libc::c_void);
if xml_raw.is_null() {
return None;
}
moz_ac.in_emaillocalpart = dc_strdup(to_cstring(&param_in.addr).as_ptr());
moz_ac.in_emaillocalpart = to_cstring(&param_in.addr);
let p = strchr(moz_ac.in_emaillocalpart, '@' as i32);
if p.is_null() {
@@ -1160,11 +1162,13 @@ unsafe fn moz_autoconfigure_text_cb(
let mut moz_ac: *mut moz_autoconfigure_t = userdata as *mut moz_autoconfigure_t;
let mut val: *mut libc::c_char = dc_strdup(text);
dc_trim(val);
let addr = to_cstring(&(*moz_ac).in_0.addr);
dc_str_replace(
&mut val,
b"%EMAILADDRESS%\x00" as *const u8 as *const libc::c_char,
to_cstring(&(*moz_ac).in_0.addr).as_ptr(),
addr,
);
free(addr as *mut libc::c_void);
dc_str_replace(
&mut val,
b"%EMAILLOCALPART%\x00" as *const u8 as *const libc::c_char,
@@ -1298,7 +1302,7 @@ fn read_autoconf_file(context: &Context, url: *const libc::c_char) -> *mut libc:
.send()
.and_then(|mut res| res.text())
{
Ok(res) => unsafe { libc::strdup(to_cstring(res).as_ptr()) },
Ok(res) => unsafe { to_cstring(res) },
Err(_err) => {
info!(context, 0, "Can\'t read file.",);
@@ -1314,7 +1318,7 @@ unsafe fn outlk_autodiscover(
) -> Option<dc_loginparam_t> {
let current_block: u64;
let mut xml_raw: *mut libc::c_char = 0 as *mut libc::c_char;
let mut url = dc_strdup(to_cstring(url__).as_ptr());
let mut url = to_cstring(url__);
let mut outlk_ad = outlk_autodiscover_t {
in_0: param_in,
out: dc_loginparam_new(),

View File

@@ -277,15 +277,12 @@ pub unsafe fn dc_contact_load_from_db(
if contact_id == 1 as libc::c_uint {
(*contact).id = contact_id;
(*contact).name = dc_stock_str((*contact).context, 2);
(*contact).addr = dc_strdup(
to_cstring(
(*contact)
.context
.sql
.get_config((*contact).context, "configured_addr")
.unwrap_or_default(),
)
.as_ptr(),
(*contact).addr = to_cstring(
(*contact)
.context
.sql
.get_config((*contact).context, "configured_addr")
.unwrap_or_default(),
);
true
} else {
@@ -294,11 +291,11 @@ pub unsafe fn dc_contact_load_from_db(
params![contact_id as i32],
|row| {
(*contact).id = contact_id;
(*contact).name = dc_strdup(to_cstring(row.get::<_, String>(0)?).as_ptr());
(*contact).addr = dc_strdup(to_cstring(row.get::<_, String>(1)?).as_ptr());
(*contact).name = to_cstring(row.get::<_, String>(0)?);
(*contact).addr = to_cstring(row.get::<_, String>(1)?);
(*contact).origin = row.get(2)?;
(*contact).blocked = row.get(3)?;
(*contact).authname = dc_strdup(to_cstring(row.get::<_, String>(4)?).as_ptr());
(*contact).authname = to_cstring(row.get::<_, String>(4)?);
Ok(())
}
).is_ok()
@@ -735,7 +732,7 @@ pub unsafe fn dc_get_contact_encrinfo(
free(fingerprint_other_verified as *mut libc::c_void);
free(fingerprint_other_unverified as *mut libc::c_void);
strdup(to_cstring(ret).as_ptr())
to_cstring(ret)
}
unsafe fn cat_fingerprint(
@@ -905,7 +902,7 @@ pub fn dc_contact_get_profile_image(contact: *const dc_contact_t) -> *mut libc::
if unsafe { (*contact).id } == 1 {
let context = unsafe { (*contact) }.context;
if let Some(avatar) = context.get_config(config::Config::Selfavatar) {
image_abs = unsafe { dc_strdup(to_cstring(avatar).as_ptr()) };
image_abs = unsafe { to_cstring(avatar) };
}
}
// TODO: else get image_abs from contact param

View File

@@ -53,7 +53,7 @@ pub unsafe fn dc_dehtml(buf_terminated: *mut libc::c_char) -> *mut libc::c_char
dc_saxparser_parse(&mut saxparser, buf_terminated);
free(dehtml.last_href as *mut libc::c_void);
strdup(to_cstring(dehtml.strbuilder).as_ptr())
to_cstring(dehtml.strbuilder)
}
unsafe fn dehtml_text_cb(

View File

@@ -184,7 +184,7 @@ pub unsafe fn dc_e2ee_encrypt(
b"Autocrypt-Gossip\x00" as *const u8
as *const libc::c_char,
),
strdup(header.as_ptr()),
header,
),
);
}

View File

@@ -309,7 +309,7 @@ pub unsafe fn dc_create_setup_code(_context: &Context) -> *mut libc::c_char {
);
}
strdup(to_cstring(ret).as_ptr())
to_cstring(ret)
}
// TODO should return bool /rtn
@@ -539,7 +539,7 @@ pub unsafe fn dc_normalize_setup_code(
p1 = p1.offset(1);
}
strdup(to_cstring(out).as_ptr())
to_cstring(out)
}
pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: *mut dc_job_t) {
@@ -895,7 +895,8 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
.format("delta-chat-%Y-%m-%d.bak")
.to_string();
let buffer = to_cstring(res);
let dest_pathNfilename = dc_get_fine_pathNfilename(context, dir, buffer.as_ptr());
let dest_pathNfilename = dc_get_fine_pathNfilename(context, dir, buffer);
free(buffer as *mut _);
if dest_pathNfilename.is_null() {
error!(context, 0, "Cannot get backup file name.",);
@@ -1094,6 +1095,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
let mut imported_cnt: libc::c_int = 0;
let mut suffix: *mut libc::c_char = 0 as *mut libc::c_char;
let mut path_plus_name: *mut libc::c_char = 0 as *mut libc::c_char;
let mut name_c: *mut libc::c_char = 0 as *mut libc::c_char;
let mut set_default: libc::c_int;
let mut buf: *mut libc::c_char = 0 as *mut libc::c_char;
let mut buf_bytes: size_t = 0 as size_t;
@@ -1121,8 +1123,9 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
let entry = entry.unwrap();
free(suffix as *mut libc::c_void);
let name_f = entry.file_name();
let name_c = to_cstring(name_f.to_string_lossy());
suffix = dc_get_filesuffix_lc(name_c.as_ptr());
free(name_c as *mut libc::c_void);
name_c = to_cstring(name_f.to_string_lossy());
suffix = dc_get_filesuffix_lc(name_c);
if suffix.is_null()
|| strcmp(suffix, b"asc\x00" as *const u8 as *const libc::c_char) != 0
{
@@ -1132,7 +1135,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
path_plus_name = dc_mprintf(
b"%s/%s\x00" as *const u8 as *const libc::c_char,
dir_name,
name_c.as_ptr(),
name_c,
);
info!(context, 0, "Checking: {}", as_str(path_plus_name));
free(buf as *mut libc::c_void);
@@ -1170,12 +1173,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
}
}
set_default = 1;
if !strstr(
name_c.as_ptr(),
b"legacy\x00" as *const u8 as *const libc::c_char,
)
.is_null()
{
if !strstr(name_c, b"legacy\x00" as *const u8 as *const libc::c_char).is_null() {
info!(
context,
0,
@@ -1200,6 +1198,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
}
}
free(name_c as *mut libc::c_void);
free(suffix as *mut libc::c_void);
free(path_plus_name as *mut libc::c_void);
free(buf as *mut libc::c_void);

View File

@@ -99,7 +99,9 @@ unsafe fn dc_job_perform(context: &Context, thread: libc::c_int, probe_network:
};
let packed: String = row.get(3)?;
dc_param_set_packed(job.param, to_cstring(packed).as_ptr());
let packed_c = to_cstring(packed);
dc_param_set_packed(job.param, packed_c);
free(packed_c as *mut _);
info!(context, 0, "DONE jobs query_maps row");
Ok(job)
},

View File

@@ -215,8 +215,10 @@ pub fn dc_get_locations(
if 0 != (*loc).msg_id {
let txt: String = row.get(9)?;
let txt_c = to_cstring(txt);
if 0 != is_marker(txt_c.as_ptr()) {
(*loc).marker = strdup(txt_c.as_ptr());
if 0 != is_marker(txt_c) {
(*loc).marker = txt_c;
} else {
free(txt_c as *mut _);
}
}
Ok(loc)
@@ -330,9 +332,9 @@ pub fn dc_get_location_kml(
}
if 0 != success {
unsafe { strdup(to_cstring(ret).as_ptr()) }
unsafe { to_cstring(ret) }
} else {
0 as *mut libc::c_char
std::ptr::null_mut()
}
}
@@ -344,7 +346,7 @@ unsafe fn get_kml_timestamp(utc: i64) -> *mut libc::c_char {
let res = chrono::NaiveDateTime::from_timestamp(utc, 0)
.format("%Y-%m-%dT%H:%M:%SZ")
.to_string();
strdup(to_cstring(res).as_ptr())
to_cstring(res)
}
pub unsafe fn dc_get_message_kml(

View File

@@ -161,24 +161,20 @@ pub unsafe fn dc_mimefactory_load_msg(
for row in rows {
let (authname, addr) = row?;
let addr_c = to_cstring(addr);
if clist_search_string_nocase(
(*factory).recipients_addr,
addr_c.as_ptr(),
) == 0
{
if clist_search_string_nocase((*factory).recipients_addr, addr_c) == 0 {
clist_insert_after(
(*factory).recipients_names,
(*(*factory).recipients_names).last,
if !authname.is_empty() {
dc_strdup(to_cstring(authname).as_ptr())
to_cstring(authname)
} else {
0 as *mut libc::c_char
std::ptr::null_mut()
} as *mut libc::c_void,
);
clist_insert_after(
(*factory).recipients_addr,
(*(*factory).recipients_addr).last,
dc_strdup(addr_c.as_ptr()) as *mut libc::c_void,
addr_c as *mut libc::c_void,
);
}
}
@@ -241,8 +237,8 @@ pub unsafe fn dc_mimefactory_load_msg(
);
match row {
Ok((in_reply_to, references)) => {
(*factory).in_reply_to = dc_strdup(to_cstring(in_reply_to).as_ptr());
(*factory).references = dc_strdup(to_cstring(references).as_ptr());
(*factory).in_reply_to = to_cstring(in_reply_to);
(*factory).references = to_cstring(references);
}
Err(err) => {
error!(
@@ -266,32 +262,24 @@ pub unsafe fn dc_mimefactory_load_msg(
unsafe fn load_from(mut factory: *mut dc_mimefactory_t) {
let context = (*factory).context;
(*factory).from_addr = strdup(
to_cstring(
context
.sql
.get_config(context, "configured_addr")
.unwrap_or_default(),
)
.as_ptr(),
(*factory).from_addr = to_cstring(
context
.sql
.get_config(context, "configured_addr")
.unwrap_or_default(),
);
(*factory).from_displayname = strdup(
to_cstring(
context
.sql
.get_config(context, "displayname")
.unwrap_or_default(),
)
.as_ptr(),
(*factory).from_displayname = to_cstring(
context
.sql
.get_config(context, "displayname")
.unwrap_or_default(),
);
(*factory).selfstatus = strdup(
to_cstring(
context
.sql
.get_config(context, "selfstatus")
.unwrap_or_default(),
)
.as_ptr(),
(*factory).selfstatus = to_cstring(
context
.sql
.get_config(context, "selfstatus")
.unwrap_or_default(),
);
if (*factory).selfstatus.is_null() {
(*factory).selfstatus = dc_stock_str((*factory).context, 13)
@@ -1178,7 +1166,7 @@ unsafe fn build_body_file(
let res = ts
.format(&format!("voice-message_%Y-%m-%d_%H-%M-%S.{}", suffix))
.to_string();
filename_to_send = strdup(to_cstring(res).as_ptr());
filename_to_send = to_cstring(res);
} else if (*msg).type_0 == 40 {
filename_to_send = dc_get_filename(pathNfilename)
} else if (*msg).type_0 == 20 || (*msg).type_0 == 21 {

View File

@@ -1341,8 +1341,9 @@ unsafe fn dc_mimeparser_add_single_part_if_known(
}
if !filename_parts.is_empty() {
free(desired_filename as *mut libc::c_void);
desired_filename =
dc_decode_ext_header(to_cstring(filename_parts).as_ptr());
let parts_c = to_cstring(filename_parts);
desired_filename = dc_decode_ext_header(parts_c);
free(parts_c as *mut _);
}
if desired_filename.is_null() {
let param = mailmime_find_ct_parameter(

View File

@@ -63,7 +63,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
ret += &format!("Cannot load message #{}.", msg_id as usize);
dc_msg_unref(msg);
dc_contact_unref(contact_from);
return strdup(to_cstring(ret).as_ptr());
return to_cstring(ret);
}
let rawtxt = rawtxt.unwrap();
let rawtxt = dc_truncate_str(rawtxt.trim(), 100000);
@@ -91,7 +91,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
// device-internal message, no further details needed
dc_msg_unref(msg);
dc_contact_unref(contact_from);
return strdup(to_cstring(ret).as_ptr());
return to_cstring(ret);
}
context
@@ -209,7 +209,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
dc_msg_unref(msg);
dc_contact_unref(contact_from);
strdup(to_cstring(ret).as_ptr())
to_cstring(ret)
}
pub unsafe fn dc_msg_new_untyped<'a>(context: &'a Context) -> *mut dc_msg_t<'a> {
@@ -445,9 +445,9 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id:
dc_msg_empty(msg);
(*msg).id = row.get::<_, i32>(0)? as u32;
(*msg).rfc724_mid = dc_strdup(to_cstring(row.get::<_, String>(1)?).as_ptr());
(*msg).in_reply_to = dc_strdup(to_cstring(row.get::<_, String>(2)?).as_ptr());
(*msg).server_folder = dc_strdup(to_cstring(row.get::<_, String>(3)?).as_ptr());
(*msg).rfc724_mid = to_cstring(row.get::<_, String>(1)?);
(*msg).in_reply_to = to_cstring(row.get::<_, String>(2)?);
(*msg).server_folder = to_cstring(row.get::<_, String>(3)?);
(*msg).server_uid = row.get(4)?;
(*msg).move_state = row.get(5)?;
(*msg).chat_id = row.get(6)?;
@@ -459,10 +459,10 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id:
(*msg).type_0 = row.get(12)?;
(*msg).state = row.get(13)?;
(*msg).is_dc_message = row.get(14)?;
(*msg).text = dc_strdup(to_cstring(row.get::<_, String>(15).unwrap_or_default()).as_ptr());
(*msg).text = to_cstring(row.get::<_, String>(15).unwrap_or_default());
dc_param_set_packed(
(*msg).param,
to_cstring(row.get::<_, String>(16)?).as_ptr()
to_cstring(row.get::<_, String>(16)?)
);
(*msg).starred = row.get(17)?;
(*msg).hidden = row.get(18)?;
@@ -494,7 +494,10 @@ pub unsafe fn dc_get_mime_headers(context: &Context, msg_id: uint32_t) -> *mut l
);
if let Some(headers) = headers {
dc_strdup_keep_null(to_cstring(headers).as_ptr())
let h = to_cstring(headers);
let res = dc_strdup_keep_null(h);
free(h as *mut _);
res
} else {
std::ptr::null_mut()
}
@@ -691,7 +694,7 @@ pub unsafe fn dc_msg_get_text(msg: *const dc_msg_t) -> *mut libc::c_char {
}
let res = dc_truncate_str(as_str((*msg).text), 30000);
dc_strdup(to_cstring(res).as_ptr())
to_cstring(res)
}
pub unsafe fn dc_msg_get_filename(msg: *const dc_msg_t) -> *mut libc::c_char {
@@ -1357,9 +1360,7 @@ pub fn dc_rfc724_mid_exists(
&[as_str(rfc724_mid)],
|row| {
if !ret_server_folder.is_null() {
unsafe {
*ret_server_folder = dc_strdup(to_cstring(row.get::<_, String>(0)?).as_ptr())
};
unsafe { *ret_server_folder = to_cstring(row.get::<_, String>(0)?) };
}
if !ret_server_uid.is_null() {
unsafe { *ret_server_uid = row.get(1)? };

View File

@@ -239,13 +239,8 @@ pub unsafe fn dc_check_qr(context: &Context, qr: *const libc::c_char) -> *mut dc
if addr.is_null() || invitenumber.is_null() || auth.is_null() {
if let Some(peerstate) = peerstate {
(*qr_parsed).state = 210i32;
let c_addr = peerstate
.addr
.as_ref()
.map(to_cstring)
.unwrap_or_default();
let addr_ptr = if peerstate.addr.is_some() {
c_addr.as_ptr()
let addr_ptr = if let Some(ref addr) = peerstate.addr {
to_cstring(addr)
} else {
std::ptr::null()
};
@@ -256,6 +251,7 @@ pub unsafe fn dc_check_qr(context: &Context, qr: *const libc::c_char) -> *mut dc
0x80i32,
0 as *mut libc::c_int,
);
free(addr_ptr as *mut _);
dc_create_or_lookup_nchat_by_contact_id(
context,
(*qr_parsed).id,

View File

@@ -739,11 +739,9 @@ pub unsafe fn dc_receive_imf(
}
if 0 != mime_parser.is_send_by_messenger || 0 != mdn_consumed {
let param = dc_param_new();
dc_param_set(
param,
'Z' as i32,
to_cstring(server_folder.as_ref()).as_ptr(),
);
let server_folder_c = to_cstring(server_folder.as_ref());
dc_param_set(param, 'Z' as i32, server_folder_c);
free(server_folder_c as *mut _);
dc_param_set_int(param, 'z' as i32, server_uid as i32);
if 0 != mime_parser.is_send_by_messenger
&& 0 != context
@@ -1518,9 +1516,7 @@ fn hex_hash(s: impl AsRef<str>) -> *const libc::c_char {
let bytes = s.as_ref().as_bytes();
let result = Sha256::digest(bytes);
let result_hex = hex::encode(&result[..8]);
let result_cstring = to_cstring(result_hex);
unsafe { strdup(result_cstring.as_ptr()) }
unsafe { to_cstring(result_hex) as *const _ }
}
unsafe fn search_chat_ids_by_contact_ids(
@@ -1604,8 +1600,7 @@ unsafe fn check_verified_properties(
let contact = dc_contact_new(context);
let verify_fail = |reason: String| {
*failure_reason =
strdup(to_cstring(format!("{}. See \"Info\" for details.", reason)).as_ptr());
*failure_reason = to_cstring(format!("{}. See \"Info\" for details.", reason));
warn!(context, 0, "{}", reason);
};

View File

@@ -62,7 +62,7 @@ pub unsafe fn dc_get_securejoin_qr(
free(group_name_urlencoded as *mut libc::c_void);
if let Some(qr) = qr {
strdup(to_cstring(qr).as_ptr())
to_cstring(qr)
} else {
std::ptr::null_mut()
}
@@ -939,15 +939,15 @@ pub unsafe fn dc_handle_degrade_event(context: &Context, peerstate: &Peerstate)
&mut contact_chat_id,
0 as *mut libc::c_int,
);
let c_addr = peerstate.addr.as_ref().map(to_cstring).unwrap_or_default();
let c_addr_ptr = if peerstate.addr.is_some() {
c_addr.as_ptr()
let c_addr_ptr = if let Some(ref addr) = peerstate.addr {
to_cstring(addr)
} else {
std::ptr::null_mut()
};
let msg = dc_stock_str_repl_string(context, 37, c_addr_ptr);
dc_add_device_msg(context, contact_chat_id, msg);
free(msg as *mut libc::c_void);
free(c_addr_ptr as *mut _);
context.call_cb(
Event::CHAT_MODIFIED,
contact_chat_id as uintptr_t,

View File

@@ -238,7 +238,7 @@ unsafe fn dc_simplify_simplify_plain_text(
}
dc_free_splitted_lines(lines);
strdup(to_cstring(ret).as_ptr())
to_cstring(ret)
}
/**

View File

@@ -712,7 +712,8 @@ unsafe fn print_hex(target: *mut libc::c_char, cur: *const libc::c_char) {
let bytes = std::slice::from_raw_parts(cur as *const _, strlen(cur));
let raw = to_cstring(format!("={}", &hex::encode_upper(bytes)[..2]));
libc::memcpy(target as *mut _, raw.as_ptr() as *const _, 4);
libc::memcpy(target as *mut _, raw as *const _, 4);
free(raw as *mut libc::c_void);
}
#[cfg(test)]

View File

@@ -1,7 +1,6 @@
use crate::context::Context;
use crate::dc_tools::*;
use crate::sql;
use crate::x::strdup;
// Token namespaces
pub type dc_tokennamespc_t = usize;
@@ -34,16 +33,16 @@ pub fn dc_token_lookup(
namespc: dc_tokennamespc_t,
foreign_id: u32,
) -> *mut libc::c_char {
if let Some(token) = context.sql.query_row_col::<_, String>(
context,
"SELECT token FROM tokens WHERE namespc=? AND foreign_id=?;",
params![namespc as i32, foreign_id as i32],
0,
) {
unsafe { strdup(to_cstring(token).as_ptr()) }
} else {
std::ptr::null_mut()
}
context
.sql
.query_row_col::<_, String>(
context,
"SELECT token FROM tokens WHERE namespc=? AND foreign_id=?;",
params![namespc as i32, foreign_id as i32],
0,
)
.map(|s| unsafe { to_cstring(s) })
.unwrap_or_else(|| std::ptr::null_mut())
}
pub fn dc_token_exists(

View File

@@ -3,11 +3,14 @@ macro_rules! info {
($ctx:expr, $data1:expr, $msg:expr) => {
info!($ctx, $data1, $msg,)
};
($ctx:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {{
let formatted = format!($msg, $($args),*);
let formatted_c = $crate::dc_tools::to_cstring(formatted);
$ctx.call_cb($crate::constants::Event::INFO, $data1 as libc::uintptr_t,
formatted_c.as_ptr() as libc::uintptr_t)
($ctx:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {
#[allow(unused_unsafe)]
unsafe {
let formatted = format!($msg, $($args),*);
let formatted_c = $crate::dc_tools::to_cstring(formatted);
$ctx.call_cb($crate::constants::Event::INFO, $data1 as libc::uintptr_t,
formatted_c as libc::uintptr_t);
libc::free(formatted_c as *mut libc::c_void);
}};
}
@@ -17,11 +20,14 @@ macro_rules! warn {
warn!($ctx, $data1, $msg,)
};
($ctx:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {
let formatted = format!($msg, $($args),*);
let formatted_c = $crate::dc_tools::to_cstring(formatted);
$ctx.call_cb($crate::constants::Event::WARNING, $data1 as libc::uintptr_t,
formatted_c.as_ptr() as libc::uintptr_t)
};
#[allow(unused_unsafe)]
unsafe {
let formatted = format!($msg, $($args),*);
let formatted_c = $crate::dc_tools::to_cstring(formatted);
$ctx.call_cb($crate::constants::Event::WARNING, $data1 as libc::uintptr_t,
formatted_c as libc::uintptr_t);
libc::free(formatted_c as *mut libc::c_void) ;
}};
}
#[macro_export]
@@ -30,11 +36,14 @@ macro_rules! error {
error!($ctx, $data1, $msg,)
};
($ctx:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {
#[allow(unused_unsafe)]
unsafe {
let formatted = format!($msg, $($args),*);
let formatted_c = $crate::dc_tools::to_cstring(formatted);
$ctx.call_cb($crate::constants::Event::ERROR, $data1 as libc::uintptr_t,
formatted_c.as_ptr() as libc::uintptr_t)
};
formatted_c as libc::uintptr_t);
libc::free(formatted_c as *mut libc::c_void);
}};
}
#[macro_export]
@@ -43,9 +52,12 @@ macro_rules! log_event {
log_event!($ctx, $data1, $msg,)
};
($ctx:expr, $event:expr, $data1:expr, $msg:expr, $($args:expr),* $(,)?) => {
let formatted = format!($msg, $($args),*);
let formatted_c = $crate::dc_tools::to_cstring(formatted);
$ctx.call_cb($event, $data1 as libc::uintptr_t,
formatted_c.as_ptr() as libc::uintptr_t)
};
#[allow(unused_unsafe)]
unsafe {
let formatted = format!($msg, $($args),*);
let formatted_c = $crate::dc_tools::to_cstring(formatted);
$ctx.call_cb($event, $data1 as libc::uintptr_t,
formatted_c as libc::uintptr_t);
libc::free(formatted_c as *mut libc::c_void);
}};
}

View File

@@ -1005,31 +1005,36 @@ pub fn housekeeping(context: &Context) {
}
let entry = entry.unwrap();
let name_f = entry.file_name();
let name_c = to_cstring(name_f.to_string_lossy());
let name_c = unsafe { to_cstring(name_f.to_string_lossy()) };
if unsafe {
is_file_in_use(&mut files_in_use, 0 as *const libc::c_char, name_c.as_ptr())
} || unsafe {
is_file_in_use(
&mut files_in_use,
b".increation\x00" as *const u8 as *const libc::c_char,
name_c.as_ptr(),
)
} || unsafe {
is_file_in_use(
&mut files_in_use,
b".waveform\x00" as *const u8 as *const libc::c_char,
name_c.as_ptr(),
)
} || unsafe {
is_file_in_use(
&mut files_in_use,
b"-preview.jpg\x00" as *const u8 as *const libc::c_char,
name_c.as_ptr(),
)
} {
if unsafe { is_file_in_use(&mut files_in_use, 0 as *const libc::c_char, name_c) }
|| unsafe {
is_file_in_use(
&mut files_in_use,
b".increation\x00" as *const u8 as *const libc::c_char,
name_c,
)
}
|| unsafe {
is_file_in_use(
&mut files_in_use,
b".waveform\x00" as *const u8 as *const libc::c_char,
name_c,
)
}
|| unsafe {
is_file_in_use(
&mut files_in_use,
b"-preview.jpg\x00" as *const u8 as *const libc::c_char,
name_c,
)
}
{
unsafe { free(name_c as *mut _) };
continue;
}
unsafe { free(name_c as *mut _) };
unreferenced_count += 1;
match std::fs::metadata(entry.path()) {
@@ -1061,8 +1066,11 @@ pub fn housekeeping(context: &Context) {
unreferenced_count,
entry.file_name()
);
let path = to_cstring(entry.path().to_str().unwrap());
unsafe { dc_delete_file(context, path.as_ptr()) };
unsafe {
let path = to_cstring(entry.path().to_str().unwrap());
dc_delete_file(context, path);
free(path as *mut _);
}
}
}
Err(err) => {
@@ -1120,14 +1128,16 @@ fn maybe_add_from_param(
context
.sql
.query_row(query, NO_PARAMS, |row| {
let v = to_cstring(row.get::<_, String>(0)?);
unsafe {
dc_param_set_packed(param, v.as_ptr() as *const libc::c_char);
let file = dc_param_get(param, param_id, 0 as *const libc::c_char);
let v = to_cstring(row.get::<_, String>(0)?);
dc_param_set_packed(param, v as *const _);
let file = dc_param_get(param, param_id, 0 as *const _);
if !file.is_null() {
maybe_add_file(files_in_use, as_str(file));
free(file as *mut libc::c_void);
}
free(v as *mut _);
}
Ok(())
})