Skip to content

Commit

Permalink
Support more than NGROUPS_MAX groups on macos
Browse files Browse the repository at this point in the history
I suspect this is the cause for our recent CI failures. Apparently,
on macos it is possible for getgroups() to return more than
NGROUPS_MAX groups. We avoid an EINVAL in that case by fetching
the exact number of groups in advance. This should work on both
macos and posix systems.
  • Loading branch information
nikic committed Apr 8, 2021
1 parent 0a181ca commit eebcfeb
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ext/posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,21 @@ PHP_FUNCTION(posix_setegid)
#ifdef HAVE_GETGROUPS
PHP_FUNCTION(posix_getgroups)
{
gid_t gidlist[NGROUPS_MAX];
gid_t *gidlist;
int result;
int i;

PHP_POSIX_NO_ARGS;

if ((result = getgroups(NGROUPS_MAX, gidlist)) < 0) {
/* MacOS may return more than NGROUPS_MAX groups.
* Fetch the actual number of groups and create an appropriate allocation. */
if ((result = getgroups(0, NULL)) < 0) {
POSIX_G(last_error) = errno;
RETURN_FALSE;
}

gidlist = emalloc(sizeof(gid_t) * result);
if ((result = getgroups(result, gidlist)) < 0) {
POSIX_G(last_error) = errno;
RETURN_FALSE;
}
Expand All @@ -579,6 +587,7 @@ PHP_FUNCTION(posix_getgroups)
for (i=0; i<result; i++) {
add_next_index_long(return_value, gidlist[i]);
}
efree(gidlist);
}
#endif
/* }}} */
Expand Down

0 comments on commit eebcfeb

Please sign in to comment.