Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix ``os.getgrouplist()``: on macOS, the ``getgrouplist()`` function 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.
19 changes: 19 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just question:
I 've understood the situation.

Is there any reason not to try with INT_MAX for the last trying?

if (ngroups == INT_MAX) {
   return PyErr_NoMemory();
} else if (ngroups > INT_MAX / 2) {
    ngroups = INT_MAX;
} else {
    ngroups *= 2;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If (ngroups > INT_MAX / 2) is false, ngroups *= 2 would overflow: ngroups type is int.

} else if (ngroups > INT_MAX / 2) {
    ngroups = INT_MAX;

I don't think that it's worth it to bother with attempting that. I don't think that any user has more than 1073741823 groups. Such user is going to get larger issues than that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got it :) Sounds reasonable.

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. */
Expand Down