Remove to_cstring() naming convention ambiguity

Add a trait for str.strdup() to replace to_cstring() which avoid the
signature ambiguity with .to_string().

Also instruduce CString::yolo() as a shortcut to
CString::new().unwrap() and use it whenever the variable does can be
deallocated by going out of scope.  This is less error prone.

Use some Path.to_c_string() functions where possible.
This commit is contained in:
Floris Bruynooghe
2019-08-01 00:08:23 +02:00
committed by Floris Bruynooghe
parent e7428887d0
commit b6b0849bce
31 changed files with 395 additions and 381 deletions

View File

@@ -671,7 +671,7 @@ fn test_encryption_decryption() {
assert!(ctext.starts_with("-----BEGIN PGP MESSAGE-----"));
let ctext_signed_bytes = ctext.len();
let ctext_signed = to_cstring(ctext);
let ctext_signed = CString::yolo(ctext);
let ctext = dc_pgp_pk_encrypt(
original_text as *const libc::c_void,
@@ -684,7 +684,7 @@ fn test_encryption_decryption() {
assert!(ctext.starts_with("-----BEGIN PGP MESSAGE-----"));
let ctext_unsigned_bytes = ctext.len();
let ctext_unsigned = to_cstring(ctext);
let ctext_unsigned = CString::yolo(ctext);
let mut keyring = Keyring::default();
keyring.add_owned(private_key);
@@ -698,7 +698,7 @@ fn test_encryption_decryption() {
let mut valid_signatures: HashSet<String> = Default::default();
let plain = dc_pgp_pk_decrypt(
ctext_signed as *const _,
ctext_signed.as_ptr() as *const _,
ctext_signed_bytes,
&keyring,
&public_keyring,
@@ -713,7 +713,7 @@ fn test_encryption_decryption() {
let empty_keyring = Keyring::default();
let plain = dc_pgp_pk_decrypt(
ctext_signed as *const _,
ctext_signed.as_ptr() as *const _,
ctext_signed_bytes,
&keyring,
&empty_keyring,
@@ -726,7 +726,7 @@ fn test_encryption_decryption() {
valid_signatures.clear();
let plain = dc_pgp_pk_decrypt(
ctext_signed as *const _,
ctext_signed.as_ptr() as *const _,
ctext_signed_bytes,
&keyring,
&public_keyring2,
@@ -741,7 +741,7 @@ fn test_encryption_decryption() {
public_keyring2.add_ref(&public_key);
let plain = dc_pgp_pk_decrypt(
ctext_signed as *const _,
ctext_signed.as_ptr() as *const _,
ctext_signed_bytes,
&keyring,
&public_keyring2,
@@ -754,7 +754,7 @@ fn test_encryption_decryption() {
valid_signatures.clear();
let plain = dc_pgp_pk_decrypt(
ctext_unsigned as *const _,
ctext_unsigned.as_ptr() as *const _,
ctext_unsigned_bytes,
&keyring,
&public_keyring,
@@ -762,7 +762,6 @@ fn test_encryption_decryption() {
)
.unwrap();
free(ctext_unsigned as *mut _);
assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),);
valid_signatures.clear();
@@ -773,7 +772,7 @@ fn test_encryption_decryption() {
public_keyring.add_ref(&public_key);
let plain = dc_pgp_pk_decrypt(
ctext_signed as *const _,
ctext_signed.as_ptr() as *const _,
ctext_signed_bytes,
&keyring,
&public_keyring,
@@ -781,7 +780,6 @@ fn test_encryption_decryption() {
)
.unwrap();
free(ctext_signed as *mut _);
assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),);
}
}
@@ -808,7 +806,7 @@ unsafe fn create_test_context() -> TestContext {
assert!(
dc_open(&mut ctx, dbfile.to_str().unwrap(), None),
"Failed to open {}",
dbfile.to_str().unwrap()
dbfile.display()
);
TestContext { ctx: ctx, dir: dir }
}
@@ -931,29 +929,24 @@ fn test_stress_tests() {
fn test_get_contacts() {
unsafe {
let context = create_test_context();
let name = to_cstring("some2");
let contacts = dc_get_contacts(&context.ctx, 0, name);
let name = CString::yolo("some2");
let contacts = dc_get_contacts(&context.ctx, 0, name.as_ptr());
assert_eq!(dc_array_get_cnt(contacts), 0);
dc_array_unref(contacts);
free(name as *mut _);
let name = to_cstring("bob");
let email = to_cstring("bob@mail.de");
let id = dc_create_contact(&context.ctx, name, email);
let name = CString::yolo("bob");
let email = CString::yolo("bob@mail.de");
let id = dc_create_contact(&context.ctx, name.as_ptr(), email.as_ptr());
assert_ne!(id, 0);
let contacts = dc_get_contacts(&context.ctx, 0, name);
let contacts = dc_get_contacts(&context.ctx, 0, name.as_ptr());
assert_eq!(dc_array_get_cnt(contacts), 1);
dc_array_unref(contacts);
let name2 = to_cstring("alice");
let contacts = dc_get_contacts(&context.ctx, 0, name2);
let name2 = CString::yolo("alice");
let contacts = dc_get_contacts(&context.ctx, 0, name2.as_ptr());
assert_eq!(dc_array_get_cnt(contacts), 0);
dc_array_unref(contacts);
free(name as *mut _);
free(name2 as *mut _);
free(email as *mut _);
}
}
@@ -961,12 +954,10 @@ fn test_get_contacts() {
fn test_chat() {
unsafe {
let context = create_test_context();
let name = to_cstring("bob");
let email = to_cstring("bob@mail.de");
let name = CString::yolo("bob");
let email = CString::yolo("bob@mail.de");
let contact1 = dc_create_contact(&context.ctx, name, email);
free(name as *mut _);
free(email as *mut _);
let contact1 = dc_create_contact(&context.ctx, name.as_ptr(), email.as_ptr());
assert_ne!(contact1, 0);
let chat_id = dc_create_chat_by_contact_id(&context.ctx, contact1);