mgetgroups: avoid argument promotion issues with -1
authorEric Blake <ebb9@byu.net>
Fri, 4 Dec 2009 21:37:32 +0000 (14:37 -0700)
committerEric Blake <ebb9@byu.net>
Sat, 5 Dec 2009 01:02:51 +0000 (18:02 -0700)
On platforms where gid_t is equivalent to uint16_t, argument
promotion states that -1 != (gid_t) -1.

* lib/mgetgroups.c (mgetgroups): A cast is required when checking
for invalid gid_t.
* tests/test-chown.h (getegid, test_chown): Likewise.
* tests/test-lchown.h (getegid, test_lchown): Likewise.

Signed-off-by: Eric Blake <ebb9@byu.net>
ChangeLog
lib/mgetgroups.c
tests/test-chown.h
tests/test-lchown.h

index da260c1..15bdded 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-12-04  Eric Blake  <ebb9@byu.net>
+
+       mgetgroups: avoid argument promotion issues with -1
+       * lib/mgetgroups.c (mgetgroups): A cast is required when checking
+       for invalid gid_t.
+       * tests/test-chown.h (getegid, test_chown): Likewise.
+       * tests/test-lchown.h (getegid, test_lchown): Likewise.
+
 2009-12-03  Paolo Bonzini  <bonzini@gnu.org>
 
        exclude: Fix header file problems.
index f68e28f..9a733d5 100644 (file)
@@ -116,7 +116,7 @@ mgetgroups (char const *username, gid_t gid, gid_t **groups)
 
   max_n_groups = (username
                   ? getugroups (0, NULL, username, gid)
-                  : getgroups (0, NULL) + (gid != -1));
+                  : getgroups (0, NULL) + (gid != (gid_t) -1));
 
   /* If we failed to count groups with NULL for a buffer,
      try again with a non-NULL one, just in case.  */
@@ -129,7 +129,7 @@ mgetgroups (char const *username, gid_t gid, gid_t **groups)
 
   ng = (username
         ? getugroups (max_n_groups, g, username, gid)
-        : getgroups (max_n_groups, g + (gid != -1)));
+        : getgroups (max_n_groups, g + (gid != (gid_t) -1)));
 
   if (ng < 0)
     {
@@ -139,7 +139,7 @@ mgetgroups (char const *username, gid_t gid, gid_t **groups)
       return -1;
     }
 
-  if (!username && gid != -1)
+  if (!username && gid != (gid_t) -1)
     {
       *g = gid;
       ng++;
index ab98682..098b77d 100644 (file)
@@ -62,7 +62,7 @@ nap (void)
 }
 
 #if !HAVE_GETEGID
-# define getegid() (-1)
+# define getegid() ((gid_t) -1)
 #endif
 
 /* This file is designed to test chown(n,o,g) and
@@ -113,8 +113,8 @@ test_chown (int (*func) (char const *, uid_t, gid_t), bool print)
 
   ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
   ASSERT (stat (BASE "dir/file", &st1) == 0);
-  ASSERT (st1.st_uid != -1);
-  ASSERT (st1.st_gid != -1);
+  ASSERT (st1.st_uid != (uid_t) -1);
+  ASSERT (st1.st_gid != (uid_t) -1);
   ASSERT (st1.st_gid == getegid ());
 
   /* Sanity check of error cases.  */
@@ -134,6 +134,7 @@ test_chown (int (*func) (char const *, uid_t, gid_t), bool print)
   /* Check that -1 does not alter ownership.  */
   ASSERT (func (BASE "dir/file", -1, st1.st_gid) == 0);
   ASSERT (func (BASE "dir/file", st1.st_uid, -1) == 0);
+  ASSERT (func (BASE "dir/file", (uid_t) -1, (gid_t) -1) == 0);
   ASSERT (stat (BASE "dir/file", &st2) == 0);
   ASSERT (st1.st_uid == st2.st_uid);
   ASSERT (st1.st_gid == st2.st_gid);
@@ -183,7 +184,7 @@ test_chown (int (*func) (char const *, uid_t, gid_t), bool print)
           gids[0] = gids[1];
         }
       ASSERT (gids[0] != st1.st_gid);
-      ASSERT (gids[0] != -1);
+      ASSERT (gids[0] != (gid_t) -1);
       ASSERT (lstat (BASE "dir/link", &st2) == 0);
       ASSERT (st1.st_uid == st2.st_uid);
       ASSERT (st1.st_gid == st2.st_gid);
index f3ddb7a..43690ac 100644 (file)
@@ -63,7 +63,7 @@ nap (void)
 #endif /* !TEST_CHOWN_NAP */
 
 #if !HAVE_GETEGID
-# define getegid() (-1)
+# define getegid() ((gid_t) -1)
 #endif
 
 #ifndef HAVE_LCHMOD
@@ -122,8 +122,8 @@ test_lchown (int (*func) (char const *, uid_t, gid_t), bool print)
 
   ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
   ASSERT (stat (BASE "dir/file", &st1) == 0);
-  ASSERT (st1.st_uid != -1);
-  ASSERT (st1.st_gid != -1);
+  ASSERT (st1.st_uid != (uid_t) -1);
+  ASSERT (st1.st_gid != (gid_t) -1);
   ASSERT (st1.st_gid == getegid ());
 
   /* Sanity check of error cases.  */
@@ -143,6 +143,7 @@ test_lchown (int (*func) (char const *, uid_t, gid_t), bool print)
   /* Check that -1 does not alter ownership.  */
   ASSERT (func (BASE "dir/file", -1, st1.st_gid) == 0);
   ASSERT (func (BASE "dir/file", st1.st_uid, -1) == 0);
+  ASSERT (func (BASE "dir/file", (uid_t) -1, (gid_t) -1) == 0);
   ASSERT (stat (BASE "dir/file", &st2) == 0);
   ASSERT (st1.st_uid == st2.st_uid);
   ASSERT (st1.st_gid == st2.st_gid);
@@ -202,7 +203,7 @@ test_lchown (int (*func) (char const *, uid_t, gid_t), bool print)
           gids[0] = gids[1];
         }
       ASSERT (gids[0] != st1.st_gid);
-      ASSERT (gids[0] != -1);
+      ASSERT (gids[0] != (gid_t) -1);
       ASSERT (lstat (BASE "dir/link", &st2) == 0);
       ASSERT (st1.st_uid == st2.st_uid);
       ASSERT (st1.st_gid == st2.st_gid);