changeset 12381:e63e3a5265e5

mgetgroups: avoid argument promotion issues with -1 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>
author Eric Blake <ebb9@byu.net>
date Fri, 04 Dec 2009 14:37:32 -0700
parents b811ff63c8e8
children 642c575d5700
files ChangeLog lib/mgetgroups.c tests/test-chown.h tests/test-lchown.h
diffstat 4 files changed, 21 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- a/lib/mgetgroups.c
+++ b/lib/mgetgroups.c
@@ -116,7 +116,7 @@
 
   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 @@
 
   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 @@
       return -1;
     }
 
-  if (!username && gid != -1)
+  if (!username && gid != (gid_t) -1)
     {
       *g = gid;
       ng++;
--- a/tests/test-chown.h
+++ b/tests/test-chown.h
@@ -62,7 +62,7 @@
 }
 
 #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 @@
 
   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 @@
   /* 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 @@
           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);
--- a/tests/test-lchown.h
+++ b/tests/test-lchown.h
@@ -63,7 +63,7 @@
 #endif /* !TEST_CHOWN_NAP */
 
 #if !HAVE_GETEGID
-# define getegid() (-1)
+# define getegid() ((gid_t) -1)
 #endif
 
 #ifndef HAVE_LCHMOD
@@ -122,8 +122,8 @@
 
   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 @@
   /* 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 @@
           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);