Skip to content

Commit 8ec7370

Browse files
authored
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.
1 parent bd409bb commit 8ec7370

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

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
@@ -6999,10 +6999,29 @@ posix_getgrouplist(PyObject *self, PyObject *args)
69996999
if (groups == NULL)
70007000
return PyErr_NoMemory();
70017001

7002+
#ifdef __APPLE__
7003+
while (getgrouplist(user, basegid, groups, &ngroups)) {
7004+
/* On macOS, getgrouplist() returns a non-zero value without setting
7005+
errno if the group list is too small. Double the list size and call
7006+
it again in this case. */
7007+
PyMem_Free(groups);
7008+
7009+
if (ngroups > INT_MAX / 2) {
7010+
return PyErr_NoMemory();
7011+
}
7012+
ngroups *= 2;
7013+
7014+
groups = PyMem_New(int, ngroups);
7015+
if (groups == NULL) {
7016+
return PyErr_NoMemory();
7017+
}
7018+
}
7019+
#else
70027020
if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
70037021
PyMem_Del(groups);
70047022
return posix_error();
70057023
}
7024+
#endif
70067025

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

0 commit comments

Comments
 (0)