From c964309d1e1ff0728580c015c45b05d2e05d5b70 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 24 Sep 2023 17:30:43 +0200 Subject: [PATCH] Add missing checks for NULL pointers --- src/unix/apple/disk.rs | 2 +- src/unix/apple/users.rs | 2 +- src/unix/apple/utils.rs | 26 -------------------------- src/unix/mod.rs | 1 + src/unix/network_helper.rs | 4 ++++ src/unix/users.rs | 13 +------------ src/unix/utils.rs | 29 +++++++++++++++++++++++++++++ 7 files changed, 37 insertions(+), 40 deletions(-) create mode 100644 src/unix/utils.rs diff --git a/src/unix/apple/disk.rs b/src/unix/apple/disk.rs index bdf8158e1..f6ebffab3 100644 --- a/src/unix/apple/disk.rs +++ b/src/unix/apple/disk.rs @@ -316,7 +316,7 @@ pub(super) unsafe fn get_str_value(dict: CFDictionaryRef, key: DictKey) -> Optio None } } else { - utils::cstr_to_rust_with_size(v_ptr, Some(len_bytes)) + crate::sys::utils::cstr_to_rust_with_size(v_ptr, Some(len_bytes)) } }) } diff --git a/src/unix/apple/users.rs b/src/unix/apple/users.rs index a2b21a3dd..b2c0e81ca 100644 --- a/src/unix/apple/users.rs +++ b/src/unix/apple/users.rs @@ -46,7 +46,7 @@ where // This is not a "real" or "local" user. continue; } - if let Some(name) = utils::cstr_to_rust((*pw).pw_name) { + if let Some(name) = crate::sys::utils::cstr_to_rust((*pw).pw_name) { if users.contains_key(&name) { continue; } diff --git a/src/unix/apple/utils.rs b/src/unix/apple/utils.rs index f8bcf6374..e87a01c09 100644 --- a/src/unix/apple/utils.rs +++ b/src/unix/apple/utils.rs @@ -35,32 +35,6 @@ impl Drop for CFReleaser { unsafe impl Send for CFReleaser {} unsafe impl Sync for CFReleaser {} -pub(crate) fn cstr_to_rust(c: *const c_char) -> Option { - cstr_to_rust_with_size(c, None) -} - -pub(crate) fn cstr_to_rust_with_size(c: *const c_char, size: Option) -> Option { - if c.is_null() { - return None; - } - let mut s = match size { - Some(len) => Vec::with_capacity(len), - None => Vec::new(), - }; - let mut i = 0; - unsafe { - loop { - let value = *c.offset(i) as u8; - if value == 0 { - break; - } - s.push(value); - i += 1; - } - String::from_utf8(s).ok() - } -} - pub(crate) fn vec_to_rust(buf: Vec) -> Option { String::from_utf8( buf.into_iter() diff --git a/src/unix/mod.rs b/src/unix/mod.rs index 5c50ede2d..8c745d64f 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -26,3 +26,4 @@ cfg_if::cfg_if! { pub(crate) mod network_helper; pub(crate) mod users; +pub(crate) mod utils; diff --git a/src/unix/network_helper.rs b/src/unix/network_helper.rs index 30352d486..d9570c1b1 100644 --- a/src/unix/network_helper.rs +++ b/src/unix/network_helper.rs @@ -22,6 +22,10 @@ impl Iterator for InterfaceAddressIterator { self.ifap = (*ifap).ifa_next; if let Some(addr) = parse_interface_address(ifap) { + let ifa_name = (*ifap).ifa_name; + if ifa_name.is_null() { + continue; + } // libc::IFNAMSIZ + 6 // This size refers to ./apple/network.rs:75 let mut name = vec![0u8; libc::IFNAMSIZ + 6]; diff --git a/src/unix/users.rs b/src/unix/users.rs index 7e5fc2b42..6a2b06dc8 100644 --- a/src/unix/users.rs +++ b/src/unix/users.rs @@ -74,18 +74,7 @@ pub(crate) unsafe fn get_group_name( break; } let g = g.assume_init(); - let mut group_name = Vec::new(); - let c_group_name = g.gr_name; - let mut x = 0; - loop { - let c = *c_group_name.offset(x); - if c == 0 { - break; - } - group_name.push(c as u8); - x += 1; - } - String::from_utf8(group_name).ok() + super::utils::cstr_to_rust(g.gr_name) } pub(crate) unsafe fn get_user_groups( diff --git a/src/unix/utils.rs b/src/unix/utils.rs new file mode 100644 index 000000000..66f2e5d73 --- /dev/null +++ b/src/unix/utils.rs @@ -0,0 +1,29 @@ +// Take a look at the license at the top of the repository in the LICENSE file. + +use libc::c_char; + +pub(crate) fn cstr_to_rust(c: *const c_char) -> Option { + cstr_to_rust_with_size(c, None) +} + +pub(crate) fn cstr_to_rust_with_size(c: *const c_char, size: Option) -> Option { + if c.is_null() { + return None; + } + let mut s = match size { + Some(len) => Vec::with_capacity(len), + None => Vec::new(), + }; + let mut i = 0; + unsafe { + loop { + let value = *c.offset(i) as u8; + if value == 0 { + break; + } + s.push(value); + i += 1; + } + String::from_utf8(s).ok() + } +}