@@ -995,17 +995,18 @@ pub fn getgroups() -> Result<Vec<Gid>> {
995995 // First get the number of groups so we can size our Vec
996996 use std:: ptr;
997997 let ret = unsafe { libc:: getgroups ( 0 , ptr:: null_mut ( ) ) } ;
998- let mut size = Errno :: result ( ret) ?;
999998
1000999 // Now actually get the groups. We try multiple times in case the number of
10011000 // groups has changed since the first call to getgroups() and the buffer is
1002- // now too small
1003- let mut groups = Vec :: < Gid > :: with_capacity ( size as usize ) ;
1001+ // now too small.
1002+ let mut groups = Vec :: < Gid > :: with_capacity ( Errno :: result ( ret ) ? as usize ) ;
10041003 loop {
10051004 // FIXME: On the platforms we currently support, the `Gid` struct has
10061005 // the same representation in memory as a bare `gid_t`. This is not
10071006 // necessarily the case on all Rust platforms, though. See RFC 1785.
1008- let ret = unsafe { libc:: getgroups ( size, groups. as_mut_ptr ( ) as * mut gid_t ) } ;
1007+ let ret = unsafe {
1008+ libc:: getgroups ( groups. capacity ( ) as c_int , groups. as_mut_ptr ( ) as * mut gid_t )
1009+ } ;
10091010
10101011 match Errno :: result ( ret) {
10111012 Ok ( s) => {
@@ -1018,7 +1019,6 @@ pub fn getgroups() -> Result<Vec<Gid>> {
10181019 let cap = groups. capacity ( ) ;
10191020 unsafe { groups. set_len ( cap) } ;
10201021 groups. reserve ( 1 ) ;
1021- size = groups. capacity ( ) as c_int ;
10221022 } ,
10231023 Err ( e) => return Err ( e)
10241024 }
@@ -1070,9 +1070,10 @@ pub fn setgroups(groups: &[Gid]) -> Result<()> {
10701070 Errno :: result ( res) . map ( |_| ( ) )
10711071}
10721072
1073- /// Calculate the supplementary group access list. Gets the group IDs of all
1074- /// groups that `user` is a member of. The additional group `group` is also
1075- /// added to the list.
1073+ /// Calculate the supplementary group access list.
1074+ ///
1075+ /// Gets the group IDs of all groups that `user` is a member of. The additional
1076+ /// group `group` is also added to the list.
10761077///
10771078/// [Further reading](http://man7.org/linux/man-pages/man3/getgrouplist.3.html)
10781079///
@@ -1108,6 +1109,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
11081109 groups. as_mut_ptr ( ) as * mut getgrouplist_group_t ,
11091110 & mut ngroups)
11101111 } ;
1112+
11111113 // BSD systems only return 0 or -1, Linux returns ngroups on success.
11121114 if ret >= 0 {
11131115 unsafe { groups. set_len ( ngroups as usize ) } ;
@@ -1134,9 +1136,11 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
11341136 }
11351137}
11361138
1137- /// Initialize the supplementary group access list. Sets the supplementary
1138- /// group IDs for the calling process using all groups that `user` is a member
1139- /// of. The additional group `group` is also added to the list.
1139+ /// Initialize the supplementary group access list.
1140+ ///
1141+ /// Sets the supplementary group IDs for the calling process using all groups
1142+ /// that `user` is a member of. The additional group `group` is also added to
1143+ /// the list.
11401144///
11411145/// [Further reading](http://man7.org/linux/man-pages/man3/initgroups.3.html)
11421146///
@@ -1146,7 +1150,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
11461150/// another user. For example, given the user `www-data`, we could look up the
11471151/// UID and GID for the user in the system's password database (usually found
11481152/// in `/etc/passwd`). If the `www-data` user's UID and GID were `33` and `33`,
1149- /// respectively, one could switch user as follows:
1153+ /// respectively, one could switch the user as follows:
11501154/// ```
11511155/// let user = CString::new("www-data").unwrap();
11521156/// let uid = Uid::from_raw(33);
0 commit comments