diff options
| author | Victor Stinner <vstinner@python.org> | 2020-03-23 20:00:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-23 20:00:57 +0100 |
| commit | 8ec7370c89aa522602eb9604086ce9f09770953d (patch) | |
| tree | ab139eeb1b37a3ceedc0ec852dea8bdeae03e045 /Modules/posixmodule.c | |
| parent | bd409bb5b78e7ccac5fcda9ab4cec770552f3090 (diff) | |
| download | cpython-git-8ec7370c89aa522602eb9604086ce9f09770953d.tar.gz | |
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.
Diffstat (limited to 'Modules/posixmodule.c')
| -rw-r--r-- | Modules/posixmodule.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index ffee87cbf1..e489b74ec7 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -6999,10 +6999,29 @@ posix_getgrouplist(PyObject *self, PyObject *args) if (groups == NULL) return PyErr_NoMemory(); +#ifdef __APPLE__ + while (getgrouplist(user, basegid, groups, &ngroups)) { + /* 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. */ + PyMem_Free(groups); + + if (ngroups > INT_MAX / 2) { + return PyErr_NoMemory(); + } + ngroups *= 2; + + groups = PyMem_New(int, ngroups); + if (groups == NULL) { + return PyErr_NoMemory(); + } + } +#else if (getgrouplist(user, basegid, groups, &ngroups) == -1) { PyMem_Del(groups); return posix_error(); } +#endif #ifdef _Py_MEMORY_SANITIZER /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
