Skip to content

uucore: correctly truncate response if getgroups shrinks #6978

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

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/uucore/src/lib/features/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ pub fn get_groups() -> IOResult<Vec<gid_t>> {
if res == -1 {
let err = IOError::last_os_error();
if err.raw_os_error() == Some(libc::EINVAL) {
// Number of groups changed, retry
// Number of groups has increased, retry
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure it is always increased ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

POSIX.1-1988 on getgroups return value:

[EINVAL]
The gidsetsize argument is non-zero and less than the number of group IDs that would have been returned.

Since ngroups is based on earlier getgroups return value, the only case where EINVAL is returned when the indicated size (ngroups parameter for getgroups) is less than what the return list would be. If the reserved space is larger than the actual list, then res != -1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, cool, thanks :)

continue;
} else {
return Err(err);
}
} else {
groups.truncate(ngroups.try_into().unwrap());
// Number of groups may have decreased
groups.truncate(res.try_into().unwrap());
return Ok(groups);
}
}
Expand Down
Loading