From 46d6df933ec6171fcfa0220dfb8f18f68293797b Mon Sep 17 00:00:00 2001 From: =?utf8?q?P=C3=A1draig=20Brady?= Date: Wed, 14 Jan 2009 19:17:39 +0000 Subject: [PATCH] simplify mgetgroups() and avoid -Wsign-compare warnings * 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 | 46 +++++++++++++++++----------------------------- src/group-list.c | 4 ++-- src/id.c | 4 ++-- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/gl/lib/mgetgroups.c b/gl/lib/mgetgroups.c index ad1fd4f..e697013 100644 --- a/gl/lib/mgetgroups.c +++ b/gl/lib/mgetgroups.c @@ -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 @@ -33,16 +33,16 @@ #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; diff --git a/src/group-list.c b/src/group-list.c index 3547ed6..46895b4 100644 --- a/src/group-list.c +++ b/src/group-list.c @@ -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); diff --git a/src/id.c b/src/id.c index 156b066..05ad2d8 100644 --- 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); -- 2.7.4