* lib/getugroups.c (getugroups): Detect getgrent failure.
authorJim Meyering <jim@meyering.net>
Fri, 6 Jul 2007 05:01:03 +0000 (05:01 +0000)
committerJim Meyering <jim@meyering.net>
Fri, 6 Jul 2007 05:01:03 +0000 (05:01 +0000)
Adjust comment to reflect reality: this function may return -1.

ChangeLog
lib/getugroups.c

index dce04db7fb13e452749ade86e9e96313f13a9983..4ea7df653aeb6b26f5a936c0404d752f2c7e1503 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2007-07-06  Jim Meyering  <jim@meyering.net>
+
+       * lib/getugroups.c (getugroups): Detect getgrent failure.
+       Adjust comment to reflect reality: this function may return -1.
+
 2007-07-05  Sergey Poznyakoff  <gray@gnu.org.ua>
 
        * build-aux/bootstrap (TP_URL,get_translations): Update to use
index 43d0650a0f5ee086b157c3aa46b3cf978495cf52..7770ff038d2127dbfb2ace8c51fb24be854b3d50 100644 (file)
@@ -45,14 +45,13 @@ struct group *getgrent ();
    process.  Store at most MAXCOUNT group IDs in the GROUPLIST array.
    If GID is not -1, store it first (if possible).  GID should be the
    group ID (pw_gid) obtained from getpwuid, in case USERNAME is not
-   listed in /etc/groups.
-   Always return the number of groups of which USERNAME is a member.  */
+   listed in /etc/groups.  Upon failure, set errno and return -1.
+   Otherwise, return the number of IDs we've written into GROUPLIST.  */
 
 int
 getugroups (int maxcount, GETGROUPS_T *grouplist, char const *username,
            gid_t gid)
 {
-  struct group *grp;
   int count = 0;
 
   if (gid != (gid_t) -1)
@@ -63,9 +62,16 @@ getugroups (int maxcount, GETGROUPS_T *grouplist, char const *username,
     }
 
   setgrent ();
-  while ((grp = getgrent ()) != 0)
+  while (1)
     {
       char **cp;
+      struct group *grp;
+
+      errno = 0;
+      grp = getgrent ();
+      if (grp == NULL)
+       break;
+
       for (cp = grp->gr_mem; *cp; ++cp)
        {
          int n;
@@ -91,13 +97,15 @@ getugroups (int maxcount, GETGROUPS_T *grouplist, char const *username,
              if (count < 0)
                {
                  errno = EOVERFLOW;
-                 count = -1;
                  goto done;
                }
            }
        }
     }
 
+  if (errno != 0)
+    count = -1;
+
  done:
   {
     int saved_errno = errno;