@@ -3631,7 +3631,12 @@ impl From<User> for libc::passwd {
3631
3631
3632
3632
#[ cfg( not( target_os = "redox" ) ) ] // RedoxFS does not support passwd
3633
3633
impl User {
3634
- fn from_anything<F >( f: F ) -> Result <Option <Self >>
3634
+ /// # Safety
3635
+ ///
3636
+ /// If `f` writes to its `*mut *mut libc::passwd` parameter, then it must
3637
+ /// also initialize the value pointed to by its `*mut libc::group`
3638
+ /// parameter.
3639
+ unsafe fn from_anything<F >( f: F ) -> Result <Option <Self >>
3635
3640
where
3636
3641
F : Fn (
3637
3642
* mut libc:: passwd,
@@ -3687,9 +3692,13 @@ impl User {
3687
3692
/// assert_eq!(res.name, "root");
3688
3693
/// ```
3689
3694
pub fn from_uid( uid: Uid ) -> Result <Option <Self >> {
3690
- User :: from_anything( |pwd, cbuf, cap, res| unsafe {
3691
- libc:: getpwuid_r( uid. 0 , pwd, cbuf, cap, res)
3692
- } )
3695
+ // SAFETY: `getpwuid_r` will write to `res` if it initializes the value
3696
+ // at `pwd`.
3697
+ unsafe {
3698
+ User :: from_anything( |pwd, cbuf, cap, res| {
3699
+ libc:: getpwuid_r( uid. 0 , pwd, cbuf, cap, res)
3700
+ } )
3701
+ }
3693
3702
}
3694
3703
3695
3704
/// Get a user by name.
@@ -3710,9 +3719,13 @@ impl User {
3710
3719
Ok ( c_str) => c_str,
3711
3720
Err ( _nul_error) => return Ok ( None ) ,
3712
3721
} ;
3713
- User :: from_anything( |pwd, cbuf, cap, res| unsafe {
3714
- libc:: getpwnam_r( name. as_ptr( ) , pwd, cbuf, cap, res)
3715
- } )
3722
+ // SAFETY: `getpwnam_r` will write to `res` if it initializes the value
3723
+ // at `pwd`.
3724
+ unsafe {
3725
+ User :: from_anything( |pwd, cbuf, cap, res| {
3726
+ libc:: getpwnam_r( name. as_ptr( ) , pwd, cbuf, cap, res)
3727
+ } )
3728
+ }
3716
3729
}
3717
3730
}
3718
3731
@@ -3763,7 +3776,12 @@ impl Group {
3763
3776
ret
3764
3777
}
3765
3778
3766
- fn from_anything<F >( f: F ) -> Result <Option <Self >>
3779
+ /// # Safety
3780
+ ///
3781
+ /// If `f` writes to its `*mut *mut libc::group` parameter, then it must
3782
+ /// also initialize the value pointed to by its `*mut libc::group`
3783
+ /// parameter.
3784
+ unsafe fn from_anything<F >( f: F ) -> Result <Option <Self >>
3767
3785
where
3768
3786
F : Fn (
3769
3787
* mut libc:: group,
@@ -3821,9 +3839,13 @@ impl Group {
3821
3839
/// assert!(res.name == "root");
3822
3840
/// ```
3823
3841
pub fn from_gid( gid: Gid ) -> Result <Option <Self >> {
3824
- Group :: from_anything( |grp, cbuf, cap, res| unsafe {
3825
- libc:: getgrgid_r( gid. 0 , grp, cbuf, cap, res)
3826
- } )
3842
+ // SAFETY: `getgrgid_r` will write to `res` if it initializes the value
3843
+ // at `grp`.
3844
+ unsafe {
3845
+ Group :: from_anything( |grp, cbuf, cap, res| {
3846
+ libc:: getgrgid_r( gid. 0 , grp, cbuf, cap, res)
3847
+ } )
3848
+ }
3827
3849
}
3828
3850
3829
3851
/// Get a group by name.
@@ -3846,9 +3868,13 @@ impl Group {
3846
3868
Ok ( c_str) => c_str,
3847
3869
Err ( _nul_error) => return Ok ( None ) ,
3848
3870
} ;
3849
- Group :: from_anything( |grp, cbuf, cap, res| unsafe {
3850
- libc:: getgrnam_r( name. as_ptr( ) , grp, cbuf, cap, res)
3851
- } )
3871
+ // SAFETY: `getgrnam_r` will write to `res` if it initializes the value
3872
+ // at `grp`.
3873
+ unsafe {
3874
+ Group :: from_anything( |grp, cbuf, cap, res| {
3875
+ libc:: getgrnam_r( name. as_ptr( ) , grp, cbuf, cap, res)
3876
+ } )
3877
+ }
3852
3878
}
3853
3879
}
3854
3880
}
0 commit comments