1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

uucore: correctly truncate response if getgroups shrinks (#6978)

The code above this line handles the case if `res` is
larger than `ngroups`, but `res < ngroups` is also a possibility,
which this line attempts to address but actually does not.

The original code resizes to `ngroups` which is a no-op (given
that `groups` is already `ngroups` size). The correct target for
re-sizing the `groups` is the result from the last `getgroups`,
i.e., `res`.
This commit is contained in:
Santeri Paavolainen 2024-12-19 14:54:24 +02:00 committed by GitHub
parent 4341ae3223
commit 2ae914b268
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -83,13 +83,14 @@ pub fn get_groups() -> IOResult<Vec<gid_t>> {
if res == -1 { if res == -1 {
let err = IOError::last_os_error(); let err = IOError::last_os_error();
if err.raw_os_error() == Some(libc::EINVAL) { if err.raw_os_error() == Some(libc::EINVAL) {
// Number of groups changed, retry // Number of groups has increased, retry
continue; continue;
} else { } else {
return Err(err); return Err(err);
} }
} else { } else {
groups.truncate(ngroups.try_into().unwrap()); // Number of groups may have decreased
groups.truncate(res.try_into().unwrap());
return Ok(groups); return Ok(groups);
} }
} }