mirror of
https://github.com/chatmail/core.git
synced 2026-05-07 08:56:30 +03:00
always use to_string_lossy() for converting c-strings to String
the function to_string() is removed; c-strings may always be badly formatted and this should never lead to a panic.
This commit is contained in:
@@ -25,7 +25,7 @@ use num_traits::{FromPrimitive, ToPrimitive};
|
|||||||
use deltachat::contact::Contact;
|
use deltachat::contact::Contact;
|
||||||
use deltachat::context::Context;
|
use deltachat::context::Context;
|
||||||
use deltachat::dc_tools::{
|
use deltachat::dc_tools::{
|
||||||
as_path, as_str, dc_strdup, to_string, to_string_lossy, OsStrExt, StrExt,
|
as_path, as_str, dc_strdup, to_string_lossy, OsStrExt, StrExt,
|
||||||
};
|
};
|
||||||
use deltachat::*;
|
use deltachat::*;
|
||||||
|
|
||||||
@@ -381,8 +381,8 @@ pub unsafe extern "C" fn dc_get_oauth2_url(
|
|||||||
return ptr::null_mut(); // NULL explicitly defined as "unknown"
|
return ptr::null_mut(); // NULL explicitly defined as "unknown"
|
||||||
}
|
}
|
||||||
let ffi_context = &*context;
|
let ffi_context = &*context;
|
||||||
let addr = to_string(addr);
|
let addr = to_string_lossy(addr);
|
||||||
let redirect = to_string(redirect);
|
let redirect = to_string_lossy(redirect);
|
||||||
ffi_context
|
ffi_context
|
||||||
.with_inner(|ctx| match oauth2::dc_get_oauth2_url(ctx, addr, redirect) {
|
.with_inner(|ctx| match oauth2::dc_get_oauth2_url(ctx, addr, redirect) {
|
||||||
Some(res) => res.strdup(),
|
Some(res) => res.strdup(),
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ fn poke_spec(context: &Context, spec: *const libc::c_char) -> libc::c_int {
|
|||||||
|
|
||||||
/* if `spec` is given, remember it for later usage; if it is not given, try to use the last one */
|
/* if `spec` is given, remember it for later usage; if it is not given, try to use the last one */
|
||||||
if !spec.is_null() {
|
if !spec.is_null() {
|
||||||
real_spec = to_string(spec);
|
real_spec = to_string_lossy(spec);
|
||||||
context
|
context
|
||||||
.sql
|
.sql
|
||||||
.set_raw_config(context, "import_spec", Some(&real_spec))
|
.set_raw_config(context, "import_spec", Some(&real_spec))
|
||||||
|
|||||||
@@ -164,7 +164,7 @@ unsafe fn outlk_autodiscover_endtag_cb(event: &BytesEnd, outlk_ad: &mut outlk_au
|
|||||||
) == 0
|
) == 0
|
||||||
&& outlk_ad.out_imap_set == 0
|
&& outlk_ad.out_imap_set == 0
|
||||||
{
|
{
|
||||||
outlk_ad.out.mail_server = to_string(outlk_ad.config[2]);
|
outlk_ad.out.mail_server = to_string_lossy(outlk_ad.config[2]);
|
||||||
outlk_ad.out.mail_port = port;
|
outlk_ad.out.mail_port = port;
|
||||||
if 0 != ssl_on {
|
if 0 != ssl_on {
|
||||||
outlk_ad.out.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32
|
outlk_ad.out.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32
|
||||||
@@ -178,7 +178,7 @@ unsafe fn outlk_autodiscover_endtag_cb(event: &BytesEnd, outlk_ad: &mut outlk_au
|
|||||||
) == 0
|
) == 0
|
||||||
&& outlk_ad.out_smtp_set == 0
|
&& outlk_ad.out_smtp_set == 0
|
||||||
{
|
{
|
||||||
outlk_ad.out.send_server = to_string(outlk_ad.config[2]);
|
outlk_ad.out.send_server = to_string_lossy(outlk_ad.config[2]);
|
||||||
outlk_ad.out.send_port = port;
|
outlk_ad.out.send_port = port;
|
||||||
if 0 != ssl_on {
|
if 0 != ssl_on {
|
||||||
outlk_ad.out.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32
|
outlk_ad.out.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32
|
||||||
|
|||||||
@@ -890,7 +890,7 @@ impl<'a> MimeParser<'a> {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let fld_message_id = (*field).fld_data.fld_message_id;
|
let fld_message_id = (*field).fld_data.fld_message_id;
|
||||||
if !fld_message_id.is_null() {
|
if !fld_message_id.is_null() {
|
||||||
return Some(to_string((*fld_message_id).mid_value));
|
return Some(to_string_lossy((*fld_message_id).mid_value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -688,8 +688,8 @@ unsafe fn add_parts(
|
|||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
to_string(mime_in_reply_to),
|
to_string_lossy(mime_in_reply_to),
|
||||||
to_string(mime_references),
|
to_string_lossy(mime_references),
|
||||||
])?;
|
])?;
|
||||||
|
|
||||||
txt_raw = None;
|
txt_raw = None;
|
||||||
@@ -1026,7 +1026,7 @@ unsafe fn create_or_lookup_group(
|
|||||||
{
|
{
|
||||||
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() {
|
||||||
grpid = to_string(dc_extract_grpid_from_rfc724_mid_list(
|
grpid = to_string_lossy(dc_extract_grpid_from_rfc724_mid_list(
|
||||||
(*fld_in_reply_to).mid_list,
|
(*fld_in_reply_to).mid_list,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@@ -1037,7 +1037,7 @@ unsafe fn create_or_lookup_group(
|
|||||||
{
|
{
|
||||||
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() {
|
||||||
grpid = to_string(dc_extract_grpid_from_rfc724_mid_list(
|
grpid = to_string_lossy(dc_extract_grpid_from_rfc724_mid_list(
|
||||||
(*fld_references).mid_list,
|
(*fld_references).mid_list,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ pub(crate) fn dc_decode_header_words(input: &str) -> String {
|
|||||||
if r as u32 != MAILIMF_NO_ERROR || out.is_null() {
|
if r as u32 != MAILIMF_NO_ERROR || out.is_null() {
|
||||||
input.to_string()
|
input.to_string()
|
||||||
} else {
|
} else {
|
||||||
let res = to_string(out);
|
let res = to_string_lossy(out);
|
||||||
free(out.cast());
|
free(out.cast());
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,11 +30,11 @@ pub(crate) fn dc_exactly_one_bit_set(v: libc::c_int) -> bool {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use deltachat::dc_tools::{dc_strdup, to_string};
|
/// use deltachat::dc_tools::{dc_strdup, to_string_lossy};
|
||||||
/// unsafe {
|
/// unsafe {
|
||||||
/// let str_a = b"foobar\x00" as *const u8 as *const libc::c_char;
|
/// let str_a = b"foobar\x00" as *const u8 as *const libc::c_char;
|
||||||
/// let str_a_copy = dc_strdup(str_a);
|
/// let str_a_copy = dc_strdup(str_a);
|
||||||
/// assert_eq!(to_string(str_a_copy), "foobar");
|
/// assert_eq!(to_string_lossy(str_a_copy), "foobar");
|
||||||
/// assert_ne!(str_a, str_a_copy);
|
/// assert_ne!(str_a, str_a_copy);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
@@ -798,22 +798,6 @@ impl<T: AsRef<str>> StrExt for T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_string(s: *const libc::c_char) -> String {
|
|
||||||
if s.is_null() {
|
|
||||||
return "".into();
|
|
||||||
}
|
|
||||||
|
|
||||||
let cstr = unsafe { CStr::from_ptr(s) };
|
|
||||||
|
|
||||||
cstr.to_str().map(|s| s.to_string()).unwrap_or_else(|err| {
|
|
||||||
panic!(
|
|
||||||
"Non utf8 string: '{:?}' ({:?})",
|
|
||||||
cstr.to_string_lossy(),
|
|
||||||
err
|
|
||||||
);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to_string_lossy(s: *const libc::c_char) -> String {
|
pub fn to_string_lossy(s: *const libc::c_char) -> String {
|
||||||
if s.is_null() {
|
if s.is_null() {
|
||||||
return "".into();
|
return "".into();
|
||||||
@@ -1436,8 +1420,8 @@ mod tests {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let res = strndup(b"helloworld\x00" as *const u8 as *const libc::c_char, 4);
|
let res = strndup(b"helloworld\x00" as *const u8 as *const libc::c_char, 4);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
to_string(res),
|
to_string_lossy(res),
|
||||||
to_string(b"hell\x00" as *const u8 as *const libc::c_char)
|
to_string_lossy(b"hell\x00" as *const u8 as *const libc::c_char)
|
||||||
);
|
);
|
||||||
assert_eq!(strlen(res), 4);
|
assert_eq!(strlen(res), 4);
|
||||||
free(res as *mut _);
|
free(res as *mut _);
|
||||||
@@ -1531,12 +1515,12 @@ mod tests {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let input = "foo\r\nbar".strdup();
|
let input = "foo\r\nbar".strdup();
|
||||||
dc_remove_cr_chars(input);
|
dc_remove_cr_chars(input);
|
||||||
assert_eq!("foo\nbar", to_string(input));
|
assert_eq!("foo\nbar", to_string_lossy(input));
|
||||||
free(input.cast());
|
free(input.cast());
|
||||||
|
|
||||||
let input = "\rfoo\r\rbar\r".strdup();
|
let input = "\rfoo\r\rbar\r".strdup();
|
||||||
dc_remove_cr_chars(input);
|
dc_remove_cr_chars(input);
|
||||||
assert_eq!("foobar", to_string(input));
|
assert_eq!("foobar", to_string_lossy(input));
|
||||||
free(input.cast());
|
free(input.cast());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -355,7 +355,7 @@ impl Message {
|
|||||||
) && buf_headerline == "-----BEGIN PGP MESSAGE-----"
|
) && buf_headerline == "-----BEGIN PGP MESSAGE-----"
|
||||||
&& !buf_setupcodebegin.is_null()
|
&& !buf_setupcodebegin.is_null()
|
||||||
{
|
{
|
||||||
return Some(to_string(buf_setupcodebegin));
|
return Some(to_string_lossy(buf_setupcodebegin));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ impl Context {
|
|||||||
if ptr.is_null() {
|
if ptr.is_null() {
|
||||||
Cow::Borrowed(id.fallback())
|
Cow::Borrowed(id.fallback())
|
||||||
} else {
|
} else {
|
||||||
let ret = to_string(ptr);
|
let ret = to_string_lossy(ptr);
|
||||||
unsafe { libc::free(ptr as *mut libc::c_void) };
|
unsafe { libc::free(ptr as *mut libc::c_void) };
|
||||||
Cow::Owned(ret)
|
Cow::Owned(ret)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ pub fn get_ct_subtype(mime: *mut Mailmime) -> Option<String> {
|
|||||||
let ct: *mut mailmime_content = (*mime).mm_content_type;
|
let ct: *mut mailmime_content = (*mime).mm_content_type;
|
||||||
|
|
||||||
if !ct.is_null() && !(*ct).ct_subtype.is_null() {
|
if !ct.is_null() && !(*ct).ct_subtype.is_null() {
|
||||||
Some(to_string((*ct).ct_subtype))
|
Some(to_string_lossy((*ct).ct_subtype))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user