Skip to content

Commit b4c8ef7

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 d561f84 commit b4c8ef7

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
@@ -6120,6 +6120,13 @@ os_getpid_impl(PyObject *module)
61206120
}
61216121
#endif /* HAVE_GETPID */
61226122

6123+
#ifdef NGROUPS_MAX
6124+
#define MAX_GROUPS NGROUPS_MAX
6125+
#else
6126+
/* defined to be 16 on Solaris7, so this should be a small number */
6127+
#define MAX_GROUPS 64
6128+
#endif
6129+
61236130
#ifdef HAVE_GETGROUPLIST
61246131

61256132
/* AC 3.5: funny apple logic below */
@@ -6132,13 +6139,6 @@ Returns a list of groups to which a user belongs.\n\n\
61326139
static PyObject *
61336140
posix_getgrouplist(PyObject *self, PyObject *args)
61346141
{
6135-
#ifdef NGROUPS_MAX
6136-
#define MAX_GROUPS NGROUPS_MAX
6137-
#else
6138-
/* defined to be 16 on Solaris7, so this should be a small number */
6139-
#define MAX_GROUPS 64
6140-
#endif
6141-
61426142
const char *user;
61436143
int i, ngroups;
61446144
PyObject *list;
@@ -6147,7 +6147,16 @@ posix_getgrouplist(PyObject *self, PyObject *args)
61476147
#else
61486148
gid_t *groups, basegid;
61496149
#endif
6150-
ngroups = MAX_GROUPS;
6150+
6151+
/*
6152+
* NGROUPS_MAX is defined by POSIX.1 as the maximum
6153+
* number of supplimental groups a users can belong to.
6154+
* We have to increment it by one because
6155+
* getgrouplist() returns both the supplemental groups
6156+
* and the primary group, i.e. all of the groups the
6157+
* user belongs to.
6158+
*/
6159+
ngroups = 1 + MAX_GROUPS;
61516160

61526161
#ifdef __APPLE__
61536162
if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
@@ -6216,13 +6225,6 @@ os_getgroups_impl(PyObject *module)
62166225
/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
62176226
{
62186227
PyObject *result = NULL;
6219-
6220-
#ifdef NGROUPS_MAX
6221-
#define MAX_GROUPS NGROUPS_MAX
6222-
#else
6223-
/* defined to be 16 on Solaris7, so this should be a small number */
6224-
#define MAX_GROUPS 64
6225-
#endif
62266228
gid_t grouplist[MAX_GROUPS];
62276229

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

0 commit comments

Comments
 (0)