Skip to content

Commit e4814dd

Browse files
committed
unistd: getgrouplist: Rework code to use reserve_double_buffer_size
The buffer resize logic can be simplified reusing the `reserve_double_buffer_size` method. Signed-off-by: Otavio Salvador <[email protected]>
1 parent 2b07f8d commit e4814dd

File tree

1 file changed

+2
-13
lines changed

1 file changed

+2
-13
lines changed

src/unistd.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,19 +1486,8 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
14861486
// BSD systems will still fill the groups buffer with as many
14871487
// groups as possible, but Linux manpages do not mention this
14881488
// behavior.
1489-
1490-
let cap = groups.capacity();
1491-
if cap >= ngroups_max as usize {
1492-
// We already have the largest capacity we can, give up
1493-
return Err(Error::invalid_argument());
1494-
}
1495-
1496-
// Reserve space for at least ngroups
1497-
groups.reserve(ngroups as usize);
1498-
1499-
// Even if the buffer gets resized to bigger than ngroups_max,
1500-
// don't ever ask for more than ngroups_max groups
1501-
ngroups = min(ngroups_max, groups.capacity() as c_int);
1489+
reserve_double_buffer_size(&mut groups, ngroups_max as usize)
1490+
.or_else(|_| Err(Error::invalid_argument()))?;
15021491
}
15031492
}
15041493
}

0 commit comments

Comments
 (0)