summaryrefslogtreecommitdiff
path: root/src/unistd.rs
diff options
context:
space:
mode:
authorOtavio Salvador <otavio@ossystems.com.br>2019-09-25 17:32:11 -0300
committerOtavio Salvador <otavio@ossystems.com.br>2019-09-25 17:44:26 -0300
commite4814dd8569832e33dd8d2e462e2351b58cf1a2f (patch)
treed7591212592b560d5ea9e6bc1a1fa0f08ae91034 /src/unistd.rs
parent2b07f8d84a03af7208dc08e0687bbea4653a8395 (diff)
downloadnix-e4814dd8569832e33dd8d2e462e2351b58cf1a2f.zip
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 <otavio@ossystems.com.br>
Diffstat (limited to 'src/unistd.rs')
-rw-r--r--src/unistd.rs15
1 files changed, 2 insertions, 13 deletions
diff --git a/src/unistd.rs b/src/unistd.rs
index dfe91e32..fcbe1939 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -1486,19 +1486,8 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
// BSD systems will still fill the groups buffer with as many
// groups as possible, but Linux manpages do not mention this
// behavior.
-
- let cap = groups.capacity();
- if cap >= ngroups_max as usize {
- // We already have the largest capacity we can, give up
- return Err(Error::invalid_argument());
- }
-
- // Reserve space for at least ngroups
- groups.reserve(ngroups as usize);
-
- // Even if the buffer gets resized to bigger than ngroups_max,
- // don't ever ask for more than ngroups_max groups
- ngroups = min(ngroups_max, groups.capacity() as c_int);
+ reserve_double_buffer_size(&mut groups, ngroups_max as usize)
+ .or_else(|_| Err(Error::invalid_argument()))?;
}
}
}