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 c2ce2fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/999999.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `::unistd::Group::members` crash on misaligned read by read_unaligned use
19 changes: 15 additions & 4 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,6 +3568,18 @@ impl Group {
///
/// # Examples
///
// Disable this test on all OS except Apple as everyone group may not exist.
#[cfg_attr(not(apple_targets), doc = " ```no_run")]
#[cfg_attr(apple_targets, doc = " ```")]
/// use nix::unistd::Group;
/// // Returns an Result<Option<Group>>, thus the double unwrap.
/// let group = Group::from_name("everyone").unwrap().unwrap();
/// assert!(group.name == "everyone");
/// let group_id = group.gid;
/// let group = Group::from_gid(group_id).unwrap().unwrap();
/// assert_eq!(group.gid, group_id);
/// assert_eq!(group.name, "everyone");
/// ```
// 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 = " ```")]
Expand Down

0 comments on commit c2ce2fd

Please sign in to comment.