From 3425a9dd708056d5e12483dd917af0c035b8b5bc Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Fri, 4 Dec 2009 14:37:32 -0700 Subject: [PATCH] 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 --- ChangeLog | 8 ++++++++ lib/mgetgroups.c | 6 +++--- tests/test-chown.h | 9 +++++---- tests/test-lchown.h | 9 +++++---- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index da260c1daf..15bdded150 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2009-12-04 Eric Blake + + 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 exclude: Fix header file problems. diff --git a/lib/mgetgroups.c b/lib/mgetgroups.c index f68e28f798..9a733d5984 100644 --- a/lib/mgetgroups.c +++ b/lib/mgetgroups.c @@ -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++; diff --git a/tests/test-chown.h b/tests/test-chown.h index ab98682e2c..098b77d22b 100644 --- a/tests/test-chown.h +++ b/tests/test-chown.h @@ -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); diff --git a/tests/test-lchown.h b/tests/test-lchown.h index f3ddb7a98b..43690ac4c6 100644 --- a/tests/test-lchown.h +++ b/tests/test-lchown.h @@ -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); -- 2.30.2