Skip to content

Commit

Permalink
Add missing checks for NULL pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Sep 24, 2023
1 parent e249f75 commit c964309
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/unix/apple/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Check failure on line 319 in src/unix/apple/disk.rs

View workflow job for this annotation

GitHub Actions / Check stable / x86_64-apple-darwin

cannot find function `cstr_to_rust_with_size` in module `crate::sys::utils`

Check failure on line 319 in src/unix/apple/disk.rs

View workflow job for this annotation

GitHub Actions / Check nightly / x86_64-apple-darwin

cannot find function `cstr_to_rust_with_size` in module `crate::sys::utils`

Check failure on line 319 in src/unix/apple/disk.rs

View workflow job for this annotation

GitHub Actions / Check nightly / aarch64-apple-ios

cannot find function `cstr_to_rust_with_size` in module `crate::sys::utils`

Check failure on line 319 in src/unix/apple/disk.rs

View workflow job for this annotation

GitHub Actions / Check stable / aarch64-apple-ios

cannot find function `cstr_to_rust_with_size` in module `crate::sys::utils`

Check failure on line 319 in src/unix/apple/disk.rs

View workflow job for this annotation

GitHub Actions / Check stable / x86_64-apple-ios

cannot find function `cstr_to_rust_with_size` in module `crate::sys::utils`

Check failure on line 319 in src/unix/apple/disk.rs

View workflow job for this annotation

GitHub Actions / Check nightly / x86_64-apple-ios

cannot find function `cstr_to_rust_with_size` in module `crate::sys::utils`
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/unix/apple/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check failure on line 49 in src/unix/apple/users.rs

View workflow job for this annotation

GitHub Actions / Check stable / x86_64-apple-darwin

cannot find function `cstr_to_rust` in module `crate::sys::utils`

Check failure on line 49 in src/unix/apple/users.rs

View workflow job for this annotation

GitHub Actions / Check nightly / x86_64-apple-darwin

cannot find function `cstr_to_rust` in module `crate::sys::utils`

Check failure on line 49 in src/unix/apple/users.rs

View workflow job for this annotation

GitHub Actions / Check nightly / aarch64-apple-ios

cannot find function `cstr_to_rust` in module `crate::sys::utils`

Check failure on line 49 in src/unix/apple/users.rs

View workflow job for this annotation

GitHub Actions / Check stable / aarch64-apple-ios

cannot find function `cstr_to_rust` in module `crate::sys::utils`

Check failure on line 49 in src/unix/apple/users.rs

View workflow job for this annotation

GitHub Actions / Check stable / x86_64-apple-ios

cannot find function `cstr_to_rust` in module `crate::sys::utils`

Check failure on line 49 in src/unix/apple/users.rs

View workflow job for this annotation

GitHub Actions / Check nightly / x86_64-apple-ios

cannot find function `cstr_to_rust` in module `crate::sys::utils`
if users.contains_key(&name) {
continue;
}
Expand Down
26 changes: 0 additions & 26 deletions src/unix/apple/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,6 @@ impl<T> Drop for CFReleaser<T> {
unsafe impl<T> Send for CFReleaser<T> {}
unsafe impl<T> Sync for CFReleaser<T> {}

pub(crate) fn cstr_to_rust(c: *const c_char) -> Option<String> {
cstr_to_rust_with_size(c, None)
}

pub(crate) fn cstr_to_rust_with_size(c: *const c_char, size: Option<usize>) -> Option<String> {
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<i8>) -> Option<String> {
String::from_utf8(
buf.into_iter()
Expand Down
1 change: 1 addition & 0 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ cfg_if::cfg_if! {

pub(crate) mod network_helper;
pub(crate) mod users;
pub(crate) mod utils;
4 changes: 4 additions & 0 deletions src/unix/network_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
13 changes: 1 addition & 12 deletions src/unix/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
29 changes: 29 additions & 0 deletions src/unix/utils.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
cstr_to_rust_with_size(c, None)
}

pub(crate) fn cstr_to_rust_with_size(c: *const c_char, size: Option<usize>) -> Option<String> {
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()
}
}

0 comments on commit c964309

Please sign in to comment.