diff --git a/changelog/999999.fixed.md b/changelog/999999.fixed.md new file mode 100644 index 0000000000..6fa39815f9 --- /dev/null +++ b/changelog/999999.fixed.md @@ -0,0 +1 @@ +Fixed `::unistd::Group::members` crash on misaligned read by read_unaligned use diff --git a/src/unistd.rs b/src/unistd.rs index 3d98a0efe8..ff7f54dcb9 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -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 @@ -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>, 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 = " ```")]