diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-01-29 15:29:58 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-01-29 17:41:26 +0100 |
commit | 0bb0351cce1bf35b62eeb7aaa5aea12f4eebf556 (patch) | |
tree | fab240406c27d6569d19763731ba6a672a273bed /openpgp-ffi/src/armor.rs | |
parent | e5f3ae121f39b46cbc0416acb4ba7049f40203d2 (diff) |
openpgp-ffi: Deduplicate code.
- This also checks for internal nul bytes.
Diffstat (limited to 'openpgp-ffi/src/armor.rs')
-rw-r--r-- | openpgp-ffi/src/armor.rs | 22 |
1 files changed, 4 insertions, 18 deletions
diff --git a/openpgp-ffi/src/armor.rs b/openpgp-ffi/src/armor.rs index f1563c32..76836dee 100644 --- a/openpgp-ffi/src/armor.rs +++ b/openpgp-ffi/src/armor.rs @@ -261,8 +261,10 @@ pub extern "system" fn pgp_armor_reader_headers(errp: Option<&mut *mut ::error:: slice::from_raw_parts_mut(buf, headers.len()) }; for (i, (key, value)) in headers.iter().enumerate() { - sl[i].key = strdup(key); - sl[i].value = strdup(value); + sl[i].key = + super::strndup(key.as_bytes()).unwrap_or(ptr::null_mut()); + sl[i].value = + super::strndup(value.as_bytes()).unwrap_or(ptr::null_mut()); } *len = headers.len(); @@ -281,22 +283,6 @@ pub extern "system" fn pgp_armor_reader_headers(errp: Option<&mut *mut ::error:: result } -/// Creates a zero-terminated C string from a &str allocated using -/// malloc. -fn strdup(s: &str) -> *mut c_char { - let b = s.as_bytes(); - let len = b.len() + 1; - let dup = unsafe { - libc::malloc(len) as *mut c_char - }; - let sl = unsafe { - slice::from_raw_parts_mut(dup as *mut uint8_t, len) - }; - sl[..len-1].copy_from_slice(b); - sl[len-1] = 0; - dup -} - /// Constructs a new filter for the given type of data. /// /// A filter that applies ASCII Armor to the data written to it. |