Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unistd: getgrouplist: Rework code to use reserve_double_buffer_size #1130

Merged
merged 1 commit into from
Sep 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))?;
}
}
}
Expand Down