Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing checks for NULL pointers #1076

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::unix::utils::cstr_to_rust_with_size(v_ptr, Some(len_bytes))
}
})
}
Expand Down
3 changes: 1 addition & 2 deletions src/unix/apple/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
User,
};

use crate::sys::utils;
use libc::{c_char, endpwent, getpwent, setpwent, strlen};
use std::collections::HashMap;

Expand Down Expand Up @@ -46,7 +45,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::unix::utils::cstr_to_rust((*pw).pw_name) {
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()
}
}