Skip to content

Commit 1cdc61c

Browse files
bpo-40014: Fix os.getgrouplist() on macOS (GH-19118)
On macOS, getgrouplist() returns a non-zero value without setting errno if the group list is too small. Double the list size and call it again in this case. (cherry picked from commit 8ec7370) Co-authored-by: Victor Stinner <[email protected]>
1 parent 5765aca commit 1cdc61c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``os.getgrouplist()``: on macOS, the ``getgrouplist()`` function returns a
2+
non-zero value without setting ``errno`` if the group list is too small. Double
3+
the list size and call it again in this case.

Modules/posixmodule.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6178,10 +6178,29 @@ posix_getgrouplist(PyObject *self, PyObject *args)
61786178
if (groups == NULL)
61796179
return PyErr_NoMemory();
61806180

6181+
#ifdef __APPLE__
6182+
while (getgrouplist(user, basegid, groups, &ngroups)) {
6183+
/* On macOS, getgrouplist() returns a non-zero value without setting
6184+
errno if the group list is too small. Double the list size and call
6185+
it again in this case. */
6186+
PyMem_Free(groups);
6187+
6188+
if (ngroups > INT_MAX / 2) {
6189+
return PyErr_NoMemory();
6190+
}
6191+
ngroups *= 2;
6192+
6193+
groups = PyMem_New(int, ngroups);
6194+
if (groups == NULL) {
6195+
return PyErr_NoMemory();
6196+
}
6197+
}
6198+
#else
61816199
if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
61826200
PyMem_Del(groups);
61836201
return posix_error();
61846202
}
6203+
#endif
61856204

61866205
#ifdef _Py_MEMORY_SANITIZER
61876206
/* Clang memory sanitizer libc intercepts don't know getgrouplist. */

0 commit comments

Comments
 (0)