Skip to content

Commit eebcfeb

Browse files
committed
Support more than NGROUPS_MAX groups on macos
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.
1 parent 0a181ca commit eebcfeb

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

ext/posix/posix.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -563,13 +563,21 @@ PHP_FUNCTION(posix_setegid)
563563
#ifdef HAVE_GETGROUPS
564564
PHP_FUNCTION(posix_getgroups)
565565
{
566-
gid_t gidlist[NGROUPS_MAX];
566+
gid_t *gidlist;
567567
int result;
568568
int i;
569569

570570
PHP_POSIX_NO_ARGS;
571571

572-
if ((result = getgroups(NGROUPS_MAX, gidlist)) < 0) {
572+
/* MacOS may return more than NGROUPS_MAX groups.
573+
* Fetch the actual number of groups and create an appropriate allocation. */
574+
if ((result = getgroups(0, NULL)) < 0) {
575+
POSIX_G(last_error) = errno;
576+
RETURN_FALSE;
577+
}
578+
579+
gidlist = emalloc(sizeof(gid_t) * result);
580+
if ((result = getgroups(result, gidlist)) < 0) {
573581
POSIX_G(last_error) = errno;
574582
RETURN_FALSE;
575583
}
@@ -579,6 +587,7 @@ PHP_FUNCTION(posix_getgroups)
579587
for (i=0; i<result; i++) {
580588
add_next_index_long(return_value, gidlist[i]);
581589
}
590+
efree(gidlist);
582591
}
583592
#endif
584593
/* }}} */

0 commit comments

Comments
 (0)