mgetgroups: Avoid undefined behaviour when ng == 0.
authorBruno Haible <bruno@clisp.org>
Wed, 9 Dec 2009 13:13:08 +0000 (14:13 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 9 Dec 2009 13:13:08 +0000 (14:13 +0100)
ChangeLog
lib/mgetgroups.c

index ecc461f..4009854 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2009-12-09  Bruno Haible  <bruno@clisp.org>
+
+       * lib/mgetgroups.c (mgetgroups): Don't remove duplicates if there is at
+       most one group.
+
 2009-12-09  Simon Josefsson <simon@josefsson.org>
             Bruno Haible  <bruno@clisp.org>
 
index 5ca450f..89d1618 100644 (file)
@@ -175,19 +175,20 @@ mgetgroups (char const *username, gid_t gid, gid_t **groups)
      duplicate removal via an O(n) hash-table.  Hence, this function
      is only documented as guaranteeing no pair-wise duplicates,
      rather than returning the minimal set.  */
-  {
-    gid_t first = *g;
-    gid_t *next;
-    gid_t *sentinel = g + ng;
-
-    for (next = g + 1; next < sentinel; next++)
-      {
-        if (*next == first || *next == *g)
-          ng--;
-        else
-          *++g = *next;
-      }
-  }
+  if (1 < ng)
+    {
+      gid_t first = *g;
+      gid_t *next;
+      gid_t *groups_end = g + ng;
+
+      for (next = g + 1; next < groups_end; next++)
+        {
+          if (*next == first || *next == *g)
+            ng--;
+          else
+            *++g = *next;
+        }
+    }
 
   return ng;
 }