Skip to content

Commit

Permalink
fix: unaligned read panic on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
anonkey committed Feb 7, 2024
1 parent 64e2de0 commit 77e1305
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/2311.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `::unistd::Group::members` using read_unaligned to avoid crash on misaligned pointers
27 changes: 21 additions & 6 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3476,18 +3476,17 @@ impl Group {
let mut ret = Vec::new();

for i in 0.. {
let u = unsafe { mem.offset(i) };
if unsafe { (*u).is_null() } {
let u = unsafe { mem.offset(i).read_unaligned() };
if u.is_null() {
break;
} else {
let s = unsafe {CStr::from_ptr(*u).to_string_lossy().into_owned()};
let s = unsafe {CStr::from_ptr(u).to_string_lossy().into_owned()};
ret.push(s);
}
}

ret
}

/// # Safety
///
/// If `f` writes to its `*mut *mut libc::group` parameter, then it must
Expand Down Expand Up @@ -3569,13 +3568,29 @@ impl Group {
///
/// # Examples
///
// Disable this test on all OS except BSD as wheel group may not exist.
#[cfg_attr(not(any(apple_targets, bsd)), doc = " ```no_run")]
#[cfg_attr(any(apple_targets, bsd), doc = " ```")]
/// use nix::unistd::Group;
/// // Returns an Result<Option<Group>>, thus the double unwrap.
/// let group = Group::from_name("wheel").unwrap().unwrap();
/// assert!(group.name == "wheel");
/// let group_id = group.gid;
/// let group = Group::from_gid(group_id).unwrap().unwrap();
/// assert_eq!(group.gid, group_id);
/// assert_eq!(group.name, "wheel");
/// ```
// Disable this test on all OS except Linux as root group may not exist.
#[cfg_attr(not(target_os = "linux"), doc = " ```no_run")]
#[cfg_attr(target_os = "linux", doc = " ```")]
/// use nix::unistd::Group;
/// // Returns an Result<Option<Group>>, thus the double unwrap.
/// let res = Group::from_name("root").unwrap().unwrap();
/// assert!(res.name == "root");
/// let group = Group::from_name("root").unwrap().unwrap();
/// assert!(group.name == "root");
/// let group_id = group.gid;
/// let group = Group::from_gid(group_id).unwrap().unwrap();
/// assert_eq!(group.gid, group_id);
/// assert_eq!(group.name, "root");
/// ```
pub fn from_name(name: &str) -> Result<Option<Self>> {
let name = match CString::new(name) {
Expand Down

0 comments on commit 77e1305

Please sign in to comment.