Skip to content

Commit 77e1305

Browse files
committed
fix: unaligned read panic on macos
1 parent 64e2de0 commit 77e1305

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

changelog/2311.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed `::unistd::Group::members` using read_unaligned to avoid crash on misaligned pointers

src/unistd.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3476,18 +3476,17 @@ impl Group {
34763476
let mut ret = Vec::new();
34773477

34783478
for i in 0.. {
3479-
let u = unsafe { mem.offset(i) };
3480-
if unsafe { (*u).is_null() } {
3479+
let u = unsafe { mem.offset(i).read_unaligned() };
3480+
if u.is_null() {
34813481
break;
34823482
} else {
3483-
let s = unsafe {CStr::from_ptr(*u).to_string_lossy().into_owned()};
3483+
let s = unsafe {CStr::from_ptr(u).to_string_lossy().into_owned()};
34843484
ret.push(s);
34853485
}
34863486
}
34873487

34883488
ret
34893489
}
3490-
34913490
/// # Safety
34923491
///
34933492
/// If `f` writes to its `*mut *mut libc::group` parameter, then it must
@@ -3569,13 +3568,29 @@ impl Group {
35693568
///
35703569
/// # Examples
35713570
///
3571+
// Disable this test on all OS except BSD as wheel group may not exist.
3572+
#[cfg_attr(not(any(apple_targets, bsd)), doc = " ```no_run")]
3573+
#[cfg_attr(any(apple_targets, bsd), doc = " ```")]
3574+
/// use nix::unistd::Group;
3575+
/// // Returns an Result<Option<Group>>, thus the double unwrap.
3576+
/// let group = Group::from_name("wheel").unwrap().unwrap();
3577+
/// assert!(group.name == "wheel");
3578+
/// let group_id = group.gid;
3579+
/// let group = Group::from_gid(group_id).unwrap().unwrap();
3580+
/// assert_eq!(group.gid, group_id);
3581+
/// assert_eq!(group.name, "wheel");
3582+
/// ```
35723583
// Disable this test on all OS except Linux as root group may not exist.
35733584
#[cfg_attr(not(target_os = "linux"), doc = " ```no_run")]
35743585
#[cfg_attr(target_os = "linux", doc = " ```")]
35753586
/// use nix::unistd::Group;
35763587
/// // Returns an Result<Option<Group>>, thus the double unwrap.
3577-
/// let res = Group::from_name("root").unwrap().unwrap();
3578-
/// assert!(res.name == "root");
3588+
/// let group = Group::from_name("root").unwrap().unwrap();
3589+
/// assert!(group.name == "root");
3590+
/// let group_id = group.gid;
3591+
/// let group = Group::from_gid(group_id).unwrap().unwrap();
3592+
/// assert_eq!(group.gid, group_id);
3593+
/// assert_eq!(group.name, "root");
35793594
/// ```
35803595
pub fn from_name(name: &str) -> Result<Option<Self>> {
35813596
let name = match CString::new(name) {

0 commit comments

Comments
 (0)