]> granicus.if.org Git - python/commitdiff
Issue #17557: Fix os.getgroups() to work with the modified behavior of
authorNed Deily <nad@acm.org>
Fri, 2 Aug 2013 04:21:15 +0000 (21:21 -0700)
committerNed Deily <nad@acm.org>
Fri, 2 Aug 2013 04:21:15 +0000 (21:21 -0700)
getgroups(2) on OS X 10.8.  Original patch by Mateusz Lenik.

Misc/ACKS
Misc/NEWS
Modules/posixmodule.c

index aaa8dd4433fdc2e6994bf6e73b2cfb0040d53d19..54fa661fbe9aed983a000dff0a7101c5746e4a37 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -721,6 +721,7 @@ Petri Lehtinen
 Luke Kenneth Casson Leighton
 Tshepang Lekhonkhobe
 Marc-AndrĂ© Lemburg
+Mateusz Lenik
 John Lenton
 Kostyantyn Leschenko
 Christopher Tur Lesniewski-Laas
index 825f6b3a133fdd1ce5faf063a9ee0e6aaf5269d2..f0cbca20e7b300adc20e30a8286dc2e587f534fc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -59,6 +59,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17557: Fix os.getgroups() to work with the modified behavior of
+  getgroups(2) on OS X 10.8.  Original patch by Mateusz Lenik.
+
 - Issue #18599: Fix name attribute of _sha1.sha1() object. It now returns
   'SHA1' instead of 'SHA'.
 
index 56903c42f1092838ea964109db2a6c2b6dfc8b14..32c85b8be91ae04061b7065c7bdb0280f3847726 100644 (file)
@@ -6331,6 +6331,34 @@ posix_getgroups(PyObject *self, PyObject *noargs)
     gid_t* alt_grouplist = grouplist;
     int n;
 
+#ifdef __APPLE__
+    /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
+     * there are more groups than can fit in grouplist.  Therefore, on OS X
+     * always first call getgroups with length 0 to get the actual number
+     * of groups.
+     */
+    n = getgroups(0, NULL);
+    if (n < 0) {
+        return posix_error();
+    } else if (n <= MAX_GROUPS) {
+        /* groups will fit in existing array */
+        alt_grouplist = grouplist;
+    } else {
+        alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
+        if (alt_grouplist == NULL) {
+            errno = EINVAL;
+            return posix_error();
+        }
+    }
+
+    n = getgroups(n, alt_grouplist);
+    if (n == -1) {
+        if (alt_grouplist != grouplist) {
+            PyMem_Free(alt_grouplist);
+        }
+        return posix_error();
+    }
+#else
     n = getgroups(MAX_GROUPS, grouplist);
     if (n < 0) {
         if (errno == EINVAL) {
@@ -6357,6 +6385,8 @@ posix_getgroups(PyObject *self, PyObject *noargs)
             return posix_error();
         }
     }
+#endif
+
     result = PyList_New(n);
     if (result != NULL) {
         int i;