Skip to content

Commit c80183e

Browse files
bpo-35070: test_getgrouplist may fail on macOS if too many groups (GH-13071)
(cherry picked from commit 8725c83) Co-authored-by: Jeffrey Kintscher <[email protected]>
1 parent f9445a3 commit c80183e

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
posix.getgrouplist() now works correctly when the user belongs to
2+
NGROUPS_MAX supplemental groups. Patch by Jeffrey Kintscher.

Modules/posixmodule.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6722,6 +6722,13 @@ os_getpid_impl(PyObject *module)
67226722
}
67236723
#endif /* HAVE_GETPID */
67246724

6725+
#ifdef NGROUPS_MAX
6726+
#define MAX_GROUPS NGROUPS_MAX
6727+
#else
6728+
/* defined to be 16 on Solaris7, so this should be a small number */
6729+
#define MAX_GROUPS 64
6730+
#endif
6731+
67256732
#ifdef HAVE_GETGROUPLIST
67266733

67276734
/* AC 3.5: funny apple logic below */
@@ -6734,13 +6741,6 @@ Returns a list of groups to which a user belongs.\n\n\
67346741
static PyObject *
67356742
posix_getgrouplist(PyObject *self, PyObject *args)
67366743
{
6737-
#ifdef NGROUPS_MAX
6738-
#define MAX_GROUPS NGROUPS_MAX
6739-
#else
6740-
/* defined to be 16 on Solaris7, so this should be a small number */
6741-
#define MAX_GROUPS 64
6742-
#endif
6743-
67446744
const char *user;
67456745
int i, ngroups;
67466746
PyObject *list;
@@ -6749,7 +6749,16 @@ posix_getgrouplist(PyObject *self, PyObject *args)
67496749
#else
67506750
gid_t *groups, basegid;
67516751
#endif
6752-
ngroups = MAX_GROUPS;
6752+
6753+
/*
6754+
* NGROUPS_MAX is defined by POSIX.1 as the maximum
6755+
* number of supplimental groups a users can belong to.
6756+
* We have to increment it by one because
6757+
* getgrouplist() returns both the supplemental groups
6758+
* and the primary group, i.e. all of the groups the
6759+
* user belongs to.
6760+
*/
6761+
ngroups = 1 + MAX_GROUPS;
67536762

67546763
#ifdef __APPLE__
67556764
if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
@@ -6818,13 +6827,6 @@ os_getgroups_impl(PyObject *module)
68186827
/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
68196828
{
68206829
PyObject *result = NULL;
6821-
6822-
#ifdef NGROUPS_MAX
6823-
#define MAX_GROUPS NGROUPS_MAX
6824-
#else
6825-
/* defined to be 16 on Solaris7, so this should be a small number */
6826-
#define MAX_GROUPS 64
6827-
#endif
68286830
gid_t grouplist[MAX_GROUPS];
68296831

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

0 commit comments

Comments
 (0)