Skip to content

Commit 09a85e2

Browse files
committed
Null-check libc::group members before converting
This mirrors the approach used for the `From<&libc::passwd> for User` impl.
1 parent 15d624a commit 09a85e2

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/unistd.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3735,11 +3735,23 @@ impl From<&libc::group> for Group {
37353735
fn from(gr: &libc::group) -> Group {
37363736
unsafe {
37373737
Group {
3738-
name: CStr::from_ptr(gr.gr_name).to_string_lossy().into_owned(),
3739-
passwd: CString::new(CStr::from_ptr(gr.gr_passwd).to_bytes())
3740-
.unwrap(),
3738+
name: if gr.gr_name.is_null() {
3739+
Default::default()
3740+
} else {
3741+
CStr::from_ptr(gr.gr_name).to_string_lossy().into_owned()
3742+
},
3743+
passwd: if gr.gr_passwd.is_null() {
3744+
Default::default()
3745+
} else {
3746+
CString::new(CStr::from_ptr(gr.gr_passwd).to_bytes())
3747+
.unwrap()
3748+
},
37413749
gid: Gid::from_raw(gr.gr_gid),
3742-
mem: Group::members(gr.gr_mem),
3750+
mem: if gr.gr_mem.is_null() {
3751+
Default::default()
3752+
} else {
3753+
Group::members(gr.gr_mem)
3754+
},
37433755
}
37443756
}
37453757
}

0 commit comments

Comments
 (0)