make dc_str_from_clist safe and return a string instead of c-string -- this allows to remove some unsafe and now unneccessary cleanup code

This commit is contained in:
holger krekel
2019-10-10 01:14:13 +02:00
parent 67848e3333
commit d412ee6042
2 changed files with 16 additions and 58 deletions

View File

@@ -1,7 +1,7 @@
use std::ptr; use std::ptr;
use itertools::join; use itertools::join;
use libc::{free, strcmp}; use libc::strcmp;
use mmime::clist::*; use mmime::clist::*;
use mmime::mailimf::types::*; use mmime::mailimf::types::*;
use mmime::mailmime::content::*; use mmime::mailmime::content::*;
@@ -319,13 +319,8 @@ unsafe fn add_parts(
let mut chat_id_blocked = Blocked::Not; let mut chat_id_blocked = Blocked::Not;
let mut sort_timestamp = 0; let mut sort_timestamp = 0;
let mut rcvd_timestamp = 0; let mut rcvd_timestamp = 0;
let mut mime_in_reply_to = std::ptr::null_mut(); let mut mime_in_reply_to = String::new();
let mut mime_references = std::ptr::null_mut(); let mut mime_references = String::new();
let cleanup = |mime_in_reply_to: *mut libc::c_char, mime_references: *mut libc::c_char| {
free(mime_in_reply_to.cast());
free(mime_references.cast());
};
// collect the rest information, CC: is added to the to-list, BCC: is ignored // collect the rest information, CC: is added to the to-list, BCC: is ignored
// (we should not add BCC to groups as this would split groups. We could add them as "known contacts", // (we should not add BCC to groups as this would split groups. We could add them as "known contacts",
@@ -359,7 +354,6 @@ unsafe fn add_parts(
message::update_server_uid(context, &rfc724_mid, server_folder.as_ref(), server_uid); message::update_server_uid(context, &rfc724_mid, server_folder.as_ref(), server_uid);
} }
cleanup(mime_in_reply_to, mime_references);
bail!("Message already in DB"); bail!("Message already in DB");
} }
@@ -588,20 +582,14 @@ unsafe fn add_parts(
if let Some(field) = mime_parser.lookup_field_typ("In-Reply-To", MAILIMF_FIELD_IN_REPLY_TO) { if let Some(field) = mime_parser.lookup_field_typ("In-Reply-To", MAILIMF_FIELD_IN_REPLY_TO) {
let fld_in_reply_to = (*field).fld_data.fld_in_reply_to; let fld_in_reply_to = (*field).fld_data.fld_in_reply_to;
if !fld_in_reply_to.is_null() { if !fld_in_reply_to.is_null() {
mime_in_reply_to = dc_str_from_clist( mime_in_reply_to = dc_str_from_clist((*(*field).fld_data.fld_in_reply_to).mid_list, " ")
(*(*field).fld_data.fld_in_reply_to).mid_list,
b" \x00" as *const u8 as *const libc::c_char,
)
} }
} }
if let Some(field) = mime_parser.lookup_field_typ("References", MAILIMF_FIELD_REFERENCES) { if let Some(field) = mime_parser.lookup_field_typ("References", MAILIMF_FIELD_REFERENCES) {
let fld_references = (*field).fld_data.fld_references; let fld_references = (*field).fld_data.fld_references;
if !fld_references.is_null() { if !fld_references.is_null() {
mime_references = dc_str_from_clist( mime_references = dc_str_from_clist((*(*field).fld_data.fld_references).mid_list, " ")
(*(*field).fld_data.fld_references).mid_list,
b" \x00" as *const u8 as *const libc::c_char,
)
} }
} }
@@ -687,8 +675,8 @@ unsafe fn add_parts(
} else { } else {
None None
}, },
to_string_lossy(mime_in_reply_to), mime_in_reply_to,
to_string_lossy(mime_references), mime_references,
])?; ])?;
txt_raw = None; txt_raw = None;
@@ -698,11 +686,7 @@ unsafe fn add_parts(
} }
Ok(()) Ok(())
}, },
) )?;
.map_err(|err| {
cleanup(mime_in_reply_to, mime_references);
err
})?;
info!( info!(
context, context,
@@ -722,8 +706,6 @@ unsafe fn add_parts(
} }
} }
cleanup(mime_in_reply_to, mime_references);
Ok(()) Ok(())
} }

View File

@@ -74,36 +74,18 @@ pub(crate) fn dc_truncate(buf: &str, approx_chars: usize, do_unwrap: bool) -> Co
} }
} }
pub(crate) unsafe fn dc_str_from_clist( pub(crate) fn dc_str_from_clist(list: *const clist, delimiter: &str) -> String {
list: *const clist,
delimiter: *const libc::c_char,
) -> *mut libc::c_char {
let mut res = String::new(); let mut res = String::new();
if !list.is_null() { if !list.is_null() {
let mut cur: *mut clistiter = (*list).first; for rfc724_mid in unsafe { (*list).into_iter() } {
while !cur.is_null() { if !res.is_empty() {
let rfc724_mid = (if !cur.is_null() { res += delimiter;
(*cur).data
} else {
ptr::null_mut()
}) as *const libc::c_char;
if !rfc724_mid.is_null() {
if !res.is_empty() && !delimiter.is_null() {
res += as_str(delimiter);
}
res += as_str(rfc724_mid);
}
cur = if !cur.is_null() {
(*cur).next
} else {
ptr::null_mut()
} }
res += as_str(rfc724_mid as *const libc::c_char);
} }
} }
res
res.strdup()
} }
pub(crate) fn dc_str_to_clist(str: &str, delimiter: &str) -> *mut clist { pub(crate) fn dc_str_to_clist(str: &str, delimiter: &str) -> *mut clist {
@@ -1060,17 +1042,11 @@ mod tests {
unsafe { unsafe {
let list: *mut clist = dc_str_to_clist("foo bar test", " "); let list: *mut clist = dc_str_to_clist("foo bar test", " ");
assert_eq!((*list).count, 3); assert_eq!((*list).count, 3);
let str: *mut libc::c_char = let str = dc_str_from_clist(list, " ");
dc_str_from_clist(list, b" \x00" as *const u8 as *const libc::c_char); assert_eq!(str, "foo bar test");
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"foo bar test"
);
clist_free_content(list); clist_free_content(list);
clist_free(list); clist_free(list);
free(str as *mut libc::c_void);
} }
} }