From d9cc1a469fab2ac78a99ba6b9357051ed0deeafc Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 12 Nov 2009 09:30:38 -0700 Subject: [PATCH] getgroups: avoid calling exit rpl_getgroups should be a library function, comparable to glibc. * modules/getgroups (Depends-on): Add malloc-posix and unistd, drop xalloc. * modules/getgroups-tests (Depends-on, Makefile.am): Drop unneeded dependencies. * lib/getgroups.c (rpl_getgroups): Fail with ENOMEM rather than exiting, in the rare case of malloc failure. Signed-off-by: Eric Blake (cherry picked from commit 80074d103cc72ea8f289b82c56bd3734aac82cd7) --- ChangeLog | 8 ++++++++ lib/getgroups.c | 21 +++++++++++---------- modules/getgroups | 6 +++--- modules/getgroups-tests | 2 -- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 256cb9927..6b9e01a4f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2009-11-13 Eric Blake + getgroups: avoid calling exit + * modules/getgroups (Depends-on): Add malloc-posix and unistd, + drop xalloc. + * modules/getgroups-tests (Depends-on, Makefile.am): Drop unneeded + dependencies. + * lib/getgroups.c (rpl_getgroups): Fail with ENOMEM rather than + exiting, in the rare case of malloc failure. + getgroups: fix logic error * lib/getgroups.c (rpl_getgroups): Don't fail if current process has more than 20 groups. diff --git a/lib/getgroups.c b/lib/getgroups.c index 207a44189..e764d732f 100644 --- a/lib/getgroups.c +++ b/lib/getgroups.c @@ -20,20 +20,19 @@ #include -#undef getgroups +#include -#include -#include #include #include -#include -#include "xalloc.h" +#undef getgroups -/* On at least Ultrix 4.3 and NextStep 3.2, getgroups (0, 0) always fails. - On other systems, it returns the number of supplemental groups for the - process. This function handles that special case and lets the system- - provided function handle all others. */ +/* On at least Ultrix 4.3 and NextStep 3.2, getgroups (0, NULL) always + fails. On other systems, it returns the number of supplemental + groups for the process. This function handles that special case + and lets the system-provided function handle all others. However, + it can fail with ENOMEM if memory is tight. It is unspecified + whether the effective group id is included in the list. */ int rpl_getgroups (int n, GETGROUPS_T *group) @@ -51,7 +50,9 @@ rpl_getgroups (int n, GETGROUPS_T *group) /* No need to worry about address arithmetic overflow here, since the ancient systems that we're running on have low limits on the number of secondary groups. */ - gbuf = xmalloc (n * sizeof *gbuf); + gbuf = malloc (n * sizeof *gbuf); + if (!gbuf) + return -1; n_groups = getgroups (n, gbuf); if (n_groups == -1 ? errno != EINVAL : n_groups < n) break; diff --git a/modules/getgroups b/modules/getgroups index 16604f8f3..472d20e9c 100644 --- a/modules/getgroups +++ b/modules/getgroups @@ -6,7 +6,8 @@ lib/getgroups.c m4/getgroups.m4 Depends-on: -xalloc +malloc-posix +unistd configure.ac: gl_FUNC_GETGROUPS @@ -20,5 +21,4 @@ License: GPL Maintainer: -Jim Meyering - +Jim Meyering, Eric Blake diff --git a/modules/getgroups-tests b/modules/getgroups-tests index f879e494b..2a5262131 100644 --- a/modules/getgroups-tests +++ b/modules/getgroups-tests @@ -2,11 +2,9 @@ Files: tests/test-getgroups.c Depends-on: -progname configure.ac: Makefile.am: TESTS += test-getgroups check_PROGRAMS += test-getgroups -test_getgroups_LDADD = $(LDADD) @LIBINTL@ -- 2.11.0