]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
simplify mgetgroups() and avoid -Wsign-compare warnings
authorPádraig Brady <P@draigBrady.com>
Wed, 14 Jan 2009 19:17:39 +0000 (19:17 +0000)
committerPádraig Brady <P@draigBrady.com>
Fri, 16 Jan 2009 11:06:09 +0000 (11:06 +0000)
* gl/lib/mgetgroups.c: Avoid -Wsign-compare warning by using unsigned
types for the parameters of the new function realloc_groupbuf().
mgetgroups() was refactored to use this function rather than
explicitly allocating and copying from automatic storage itself.
* src/group-list.c: Use int rather than size_t as variable is
used in signed comparisons.
* src/id.c: ditto.

gl/lib/mgetgroups.c
src/group-list.c
src/id.c

index ad1fd4f3bcaaf3516ea52e785e24f39368e6ba78..e697013ef0ece1f73dc3a81af6ead82f0ccfb110 100644 (file)
@@ -1,6 +1,6 @@
 /* mgetgroups.c -- return a list of the groups a user is in
 
-   Copyright (C) 2007-2008 Free Software Foundation, Inc.
+   Copyright (C) 2007-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "xalloc.h"
 
 
-static void *
-allocate_groupbuf (int size)
+static GETGROUPS_T *
+realloc_groupbuf (GETGROUPS_T *g, size_t num)
 {
-  if (xalloc_oversized (size, sizeof (GETGROUPS_T)))
+  if (xalloc_oversized (num, sizeof (*g)))
     {
       errno = ENOMEM;
       return NULL;
     }
 
-  return malloc (size * sizeof (GETGROUPS_T));
+  return realloc (g, num * sizeof (*g));
 }
 
 /* Like getugroups, but store the result in malloc'd storage.
@@ -65,45 +65,27 @@ mgetgroups (char const *username, gid_t gid, GETGROUPS_T **groups)
      performance characteristics.
 
      In glibc 2.3.2, getgrouplist is buggy.  If you pass a zero as the
-     size of the output buffer, getgrouplist will still write to the
+     length of the output buffer, getgrouplist will still write to the
      buffer.  Contrary to what some versions of the getgrouplist
      manpage say, this doesn't happen with nonzero buffer sizes.
      Therefore our usage here just avoids a zero sized buffer.  */
   if (username)
     {
       enum { N_GROUPS_INIT = 10 };
-      GETGROUPS_T smallbuf[N_GROUPS_INIT];
-
       max_n_groups = N_GROUPS_INIT;
-      ng = getgrouplist (username, gid, smallbuf, &max_n_groups);
 
-      g = allocate_groupbuf (max_n_groups);
+      g = realloc_groupbuf (NULL, max_n_groups);
       if (g == NULL)
        return -1;
 
-      if (max_n_groups <= N_GROUPS_INIT)
-       {
-         /* smallbuf was big enough, so we already have our data */
-         memcpy (g, smallbuf, max_n_groups * sizeof *g);
-         *groups = g;
-         return max_n_groups;
-       }
-
       while (1)
        {
          GETGROUPS_T *h;
-         ng = getgrouplist (username, gid, g, &max_n_groups);
-         if (0 <= ng)
-           {
-             *groups = g;
-             return ng;
-           }
 
-         /* When getgrouplist fails, it guarantees that
-            max_n_groups reflects the new number of groups.  */
+         /* getgrouplist updates max_n_groups to num required.  */
+         ng = getgrouplist (username, gid, g, &max_n_groups);
 
-         if (xalloc_oversized (max_n_groups, sizeof *h)
-             || (h = realloc (g, max_n_groups * sizeof *h)) == NULL)
+         if ((h = realloc_groupbuf (g, max_n_groups)) == NULL)
            {
              int saved_errno = errno;
              free (g);
@@ -111,6 +93,12 @@ mgetgroups (char const *username, gid_t gid, GETGROUPS_T **groups)
              return -1;
            }
          g = h;
+
+         if (0 <= ng)
+           {
+             *groups = g;
+             return ng;
+           }
        }
     }
   /* else no username, so fall through and use getgroups. */
@@ -125,7 +113,7 @@ mgetgroups (char const *username, gid_t gid, GETGROUPS_T **groups)
   if (max_n_groups < 0)
       max_n_groups = 5;
 
-  g = allocate_groupbuf (max_n_groups);
+  g = realloc_groupbuf (NULL, max_n_groups);
   if (g == NULL)
     return -1;
 
index 3547ed669e9c94bd5777f05b2a4145a46104d260..46895b47456b53929cb0365bc7bf24493467c25d 100644 (file)
@@ -1,5 +1,5 @@
 /* group-list.c --Print a list of group IDs or names.
-   Copyright (C) 1989-2008 Free Software Foundation, Inc.
+   Copyright (C) 1989-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -57,7 +57,7 @@ print_group_list (const char *username,
 #if HAVE_GETGROUPS
   {
     GETGROUPS_T *groups;
-    size_t i;
+    int i;
 
     int n_groups = mgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
                                &groups);
index 156b066dd25df3a925c10785c1bd0b77db814c92..05ad2d86b40c46bc5e19a094aa3543aea704a647 100644 (file)
--- a/src/id.c
+++ b/src/id.c
@@ -1,5 +1,5 @@
 /* id -- print real and effective UIDs and GIDs
-   Copyright (C) 1989-2008 Free Software Foundation, Inc.
+   Copyright (C) 1989-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -294,7 +294,7 @@ print_full_info (const char *username)
 #if HAVE_GETGROUPS
   {
     GETGROUPS_T *groups;
-    size_t i;
+    int i;
 
     int n_groups = mgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
                               &groups);