Skip to content

bpo-35070: test_getgrouplist may fail on macOS if too many groups #13071

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 3 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
posix.getgrouplist() now works correctly when the user belongs to
NGROUPS_MAX supplemental groups. Patch by Jeffrey Kintscher.
32 changes: 17 additions & 15 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6627,6 +6627,13 @@ os_getpid_impl(PyObject *module)
}
#endif /* HAVE_GETPID */

#ifdef NGROUPS_MAX
#define MAX_GROUPS NGROUPS_MAX
#else
/* defined to be 16 on Solaris7, so this should be a small number */
#define MAX_GROUPS 64
#endif

#ifdef HAVE_GETGROUPLIST

/* AC 3.5: funny apple logic below */
Expand All @@ -6639,13 +6646,6 @@ Returns a list of groups to which a user belongs.\n\n\
static PyObject *
posix_getgrouplist(PyObject *self, PyObject *args)
{
#ifdef NGROUPS_MAX
#define MAX_GROUPS NGROUPS_MAX
#else
/* defined to be 16 on Solaris7, so this should be a small number */
#define MAX_GROUPS 64
#endif

const char *user;
int i, ngroups;
PyObject *list;
Expand All @@ -6654,7 +6654,16 @@ posix_getgrouplist(PyObject *self, PyObject *args)
#else
gid_t *groups, basegid;
#endif
ngroups = MAX_GROUPS;

/*
* NGROUPS_MAX is defined by POSIX.1 as the maximum
* number of supplimental groups a users can belong to.
* We have to increment it by one because
* getgrouplist() returns both the supplemental groups
* and the primary group, i.e. all of the groups the
* user belongs to.
*/
ngroups = 1 + MAX_GROUPS;

#ifdef __APPLE__
if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Expand Down Expand Up @@ -6723,13 +6732,6 @@ os_getgroups_impl(PyObject *module)
/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
{
PyObject *result = NULL;

#ifdef NGROUPS_MAX
#define MAX_GROUPS NGROUPS_MAX
#else
/* defined to be 16 on Solaris7, so this should be a small number */
#define MAX_GROUPS 64
#endif
gid_t grouplist[MAX_GROUPS];

/* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Expand Down