Skip to content

Commit de1ed63

Browse files
committed
Avoid using From<*mut libc::...> using reference
Signed-off-by: Otavio Salvador <[email protected]>
1 parent a8143fb commit de1ed63

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

src/unistd.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,8 +2435,8 @@ pub struct User {
24352435
pub expire: libc::time_t
24362436
}
24372437

2438-
impl From<*mut libc::passwd> for User {
2439-
fn from(pw: *mut libc::passwd) -> User {
2438+
impl From<&libc::passwd> for User {
2439+
fn from(pw: &libc::passwd) -> User {
24402440
unsafe {
24412441
User {
24422442
name: CStr::from_ptr((*pw).pw_name).to_string_lossy().into_owned(),
@@ -2488,9 +2488,11 @@ impl User {
24882488
)
24892489
};
24902490

2491+
let pwd = unsafe { pwd.assume_init() };
2492+
24912493
if error == 0 {
24922494
if ! res.is_null() {
2493-
Some(Ok(User::from(res)))
2495+
Some(Ok(User::from(&pwd)))
24942496
} else {
24952497
None
24962498
}
@@ -2528,9 +2530,11 @@ impl User {
25282530
)
25292531
};
25302532

2533+
let pwd = unsafe { pwd.assume_init() };
2534+
25312535
if error == 0 {
25322536
if ! res.is_null() {
2533-
Some(Ok(User::from(res)))
2537+
Some(Ok(User::from(&pwd)))
25342538
} else {
25352539
None
25362540
}
@@ -2551,8 +2555,8 @@ pub struct Group {
25512555
pub mem: Vec<String>
25522556
}
25532557

2554-
impl From<*mut libc::group> for Group {
2555-
fn from(gr: *mut libc::group) -> Group {
2558+
impl From<&libc::group> for Group {
2559+
fn from(gr: &libc::group) -> Group {
25562560
unsafe {
25572561
Group {
25582562
name: CStr::from_ptr((*gr).gr_name).to_string_lossy().into_owned(),
@@ -2611,9 +2615,11 @@ impl Group {
26112615
)
26122616
};
26132617

2618+
let grp = unsafe { grp.assume_init() };
2619+
26142620
if error == 0 {
26152621
if !res.is_null() {
2616-
Some(Ok(Group::from(res)))
2622+
Some(Ok(Group::from(&grp)))
26172623
} else {
26182624
None
26192625
}
@@ -2653,9 +2659,11 @@ impl Group {
26532659
)
26542660
};
26552661

2662+
let grp = unsafe { grp.assume_init() };
2663+
26562664
if error == 0 {
26572665
if !res.is_null() {
2658-
Some(Ok(Group::from(res)))
2666+
Some(Ok(Group::from(&grp)))
26592667
} else {
26602668
None
26612669
}
@@ -2719,18 +2727,24 @@ mod usergroupiter {
27192727
impl Iterator for Users {
27202728
type Item = Result<User>;
27212729
fn next(&mut self) -> Option<Result<User>> {
2722-
27232730
let mut cbuf = vec![0 as c_char; self.0];
2724-
let mut pwd: libc::passwd = unsafe { mem::uninitialized() };
2731+
let mut pwd = mem::MaybeUninit::<libc::passwd>::uninit();
27252732
let mut res = ptr::null_mut();
27262733

27272734
let error = unsafe {
27282735
Errno::clear();
2729-
libc::getpwent_r(&mut pwd, cbuf.as_mut_ptr(), self.0, &mut res)
2736+
libc::getpwent_r(
2737+
pwd.as_mut_ptr(),
2738+
cbuf.as_mut_ptr(),
2739+
self.0,
2740+
&mut res
2741+
)
27302742
};
27312743

2744+
let pwd = unsafe { pwd.assume_init() };
2745+
27322746
if error == 0 && !res.is_null() {
2733-
Some(Ok(User::from(res)))
2747+
Some(Ok(User::from(&pwd)))
27342748
} else if error == libc::ERANGE {
27352749
Some(Err(Error::Sys(Errno::last())))
27362750
} else {
@@ -2787,18 +2801,24 @@ mod usergroupiter {
27872801
impl Iterator for Groups {
27882802
type Item = Result<Group>;
27892803
fn next(&mut self) -> Option<Result<Group>> {
2790-
27912804
let mut cbuf = vec![0 as c_char; self.0];
2792-
let mut grp: libc::group = unsafe { mem::uninitialized() };
2805+
let mut grp = mem::MaybeUninit::<libc::group>::uninit();
27932806
let mut res = ptr::null_mut();
27942807

27952808
let error = unsafe {
27962809
Errno::clear();
2797-
libc::getgrent_r(&mut grp, cbuf.as_mut_ptr(), self.0, &mut res)
2810+
libc::getgrent_r(
2811+
grp.as_mut_ptr(),
2812+
cbuf.as_mut_ptr(),
2813+
self.0,
2814+
&mut res
2815+
)
27982816
};
27992817

2818+
let grp = unsafe { grp.assume_init() };
2819+
28002820
if error == 0 && !res.is_null() {
2801-
Some(Ok(Group::from(res)))
2821+
Some(Ok(Group::from(&grp)))
28022822
} else if error == libc::ERANGE {
28032823
Some(Err(Error::Sys(Errno::last())))
28042824
} else {

0 commit comments

Comments
 (0)