mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
Make dc_create_setup_code() safe (#239)
Make dc_create_setup_code() safe
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::ffi::CString;
|
||||
use std::str::FromStr;
|
||||
|
||||
use deltachat::chatlist::*;
|
||||
@@ -568,13 +569,14 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
dc_imex(context, 2, context.get_blobdir(), 0 as *const libc::c_char);
|
||||
}
|
||||
"export-setup" => {
|
||||
let setup_code: *mut libc::c_char = dc_create_setup_code(context);
|
||||
let setup_code = dc_create_setup_code(context);
|
||||
let setup_code_c = CString::new(setup_code.clone()).unwrap();
|
||||
let file_name: *mut libc::c_char = dc_mprintf(
|
||||
b"%s/autocrypt-setup-message.html\x00" as *const u8 as *const libc::c_char,
|
||||
context.get_blobdir(),
|
||||
);
|
||||
let file_content: *mut libc::c_char;
|
||||
file_content = dc_render_setup_file(context, setup_code);
|
||||
file_content = dc_render_setup_file(context, setup_code_c.as_ptr());
|
||||
if !file_content.is_null()
|
||||
&& 0 != dc_write_file(
|
||||
context,
|
||||
@@ -586,14 +588,13 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
println!(
|
||||
"Setup message written to: {}\nSetup code: {}",
|
||||
as_str(file_name),
|
||||
as_str(setup_code),
|
||||
&setup_code,
|
||||
)
|
||||
} else {
|
||||
bail!("");
|
||||
}
|
||||
free(file_content as *mut libc::c_void);
|
||||
free(file_name as *mut libc::c_void);
|
||||
free(setup_code as *mut libc::c_void);
|
||||
}
|
||||
"poke" => {
|
||||
ensure!(0 != poke_spec(context, arg1_c), "Poke failed");
|
||||
|
||||
@@ -105,7 +105,7 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
|
||||
if 0 == dc_alloc_ongoing(context) {
|
||||
return 0 as *mut libc::c_char;
|
||||
}
|
||||
setup_code = dc_create_setup_code(context);
|
||||
setup_code = to_cstring(dc_create_setup_code(context));
|
||||
if !setup_code.is_null() {
|
||||
/* this may require a keypair to be created. this may take a second ... */
|
||||
if !context
|
||||
@@ -282,7 +282,7 @@ pub unsafe extern "C" fn dc_render_setup_file(
|
||||
ret_setupfilecontent
|
||||
}
|
||||
|
||||
pub unsafe fn dc_create_setup_code(_context: &Context) -> *mut libc::c_char {
|
||||
pub fn dc_create_setup_code(_context: &Context) -> String {
|
||||
let mut random_val: uint16_t;
|
||||
let mut rng = thread_rng();
|
||||
let mut ret = String::new();
|
||||
@@ -302,7 +302,7 @@ pub unsafe fn dc_create_setup_code(_context: &Context) -> *mut libc::c_char {
|
||||
);
|
||||
}
|
||||
|
||||
to_cstring(ret)
|
||||
ret
|
||||
}
|
||||
|
||||
// TODO should return bool /rtn
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Stress some functions for testing; if used as a lib, this file is obsolete.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::ffi::CString;
|
||||
|
||||
use mmime::mailimf_types::*;
|
||||
use tempfile::{tempdir, TempDir};
|
||||
@@ -527,11 +528,11 @@ unsafe fn stress_functions(context: &Context) {
|
||||
);
|
||||
free(buf_1 as *mut libc::c_void);
|
||||
if 0 != dc_is_configured(context) {
|
||||
let setupcode: *mut libc::c_char;
|
||||
let setupfile: *mut libc::c_char;
|
||||
setupcode = dc_create_setup_code(context);
|
||||
assert!(!setupcode.is_null());
|
||||
assert_eq!(strlen(setupcode), 44);
|
||||
let setupcode_c =
|
||||
CString::new(dc_create_setup_code(context)).expect("invalid string converted");
|
||||
assert_eq!(setupcode_c.to_bytes().len(), 44);
|
||||
let setupcode = setupcode_c.as_ptr();
|
||||
assert!(
|
||||
0 != !(*setupcode.offset(4isize) as libc::c_int == '-' as i32
|
||||
&& *setupcode.offset(9isize) as libc::c_int == '-' as i32
|
||||
@@ -596,7 +597,6 @@ unsafe fn stress_functions(context: &Context) {
|
||||
);
|
||||
free(payload as *mut libc::c_void);
|
||||
free(setupfile as *mut libc::c_void);
|
||||
free(setupcode as *mut libc::c_void);
|
||||
}
|
||||
|
||||
if 0 != dc_is_configured(context) {
|
||||
|
||||
Reference in New Issue
Block a user