changeset 12411:bff2e832acad

mgetgroups: Avoid undefined behaviour when ng == 0.
author Bruno Haible <bruno@clisp.org>
date Wed, 09 Dec 2009 14:13:08 +0100
parents 481fcf53f002
children 1718a15bc893
files ChangeLog lib/mgetgroups.c
diffstat 2 files changed, 18 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- 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>
 
--- a/lib/mgetgroups.c
+++ b/lib/mgetgroups.c
@@ -175,19 +175,20 @@
      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;
+  if (1 < ng)
+    {
+      gid_t first = *g;
+      gid_t *next;
+      gid_t *groups_end = g + ng;
 
-    for (next = g + 1; next < sentinel; next++)
-      {
-        if (*next == first || *next == *g)
-          ng--;
-        else
-          *++g = *next;
-      }
-  }
+      for (next = g + 1; next < groups_end; next++)
+        {
+          if (*next == first || *next == *g)
+            ng--;
+          else
+            *++g = *next;
+        }
+    }
 
   return ng;
 }