Skip to content

Commit 21bee0b

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 c959fa9 commit 21bee0b

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
@@ -6914,10 +6914,29 @@ posix_getgrouplist(PyObject *self, PyObject *args)
69146914
if (groups == NULL)
69156915
return PyErr_NoMemory();
69166916

6917+
#ifdef __APPLE__
6918+
while (getgrouplist(user, basegid, groups, &ngroups)) {
6919+
/* On macOS, getgrouplist() returns a non-zero value without setting
6920+
errno if the group list is too small. Double the list size and call
6921+
it again in this case. */
6922+
PyMem_Free(groups);
6923+
6924+
if (ngroups > INT_MAX / 2) {
6925+
return PyErr_NoMemory();
6926+
}
6927+
ngroups *= 2;
6928+
6929+
groups = PyMem_New(int, ngroups);
6930+
if (groups == NULL) {
6931+
return PyErr_NoMemory();
6932+
}
6933+
}
6934+
#else
69176935
if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
69186936
PyMem_Del(groups);
69196937
return posix_error();
69206938
}
6939+
#endif
69216940

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

0 commit comments

Comments
 (0)