Skip to content

Commit 27a24d4

Browse files
committed
fix: unaligned read panic on macos
1 parent 64e2de0 commit 27a24d4

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
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` crash on misaligned read by read_unaligned use

src/unistd.rs

Lines changed: 15 additions & 4 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,6 +3568,18 @@ impl Group {
35693568
///
35703569
/// # Examples
35713570
///
3571+
// Disable this test on all OS except Apple as everyone group may not exist.
3572+
#[cfg_attr(not(apple_targets), doc = " ```no_run")]
3573+
#[cfg_attr(apple_targets, doc = " ```")]
3574+
/// use nix::unistd::Group;
3575+
/// // Returns an Result<Option<Group>>, thus the double unwrap.
3576+
/// let group = Group::from_name("everyone").unwrap().unwrap();
3577+
/// assert!(group.name == "everyone");
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, "everyone");
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 = " ```")]

0 commit comments

Comments
 (0)