summaryrefslogtreecommitdiff
path: root/lib/getgroups.c
diff options
context:
space:
mode:
authorEric Blake <ebb9@byu.net>2009-11-12 09:30:38 -0700
committerEric Blake <ebb9@byu.net>2009-11-13 07:39:01 -0700
commit80074d103cc72ea8f289b82c56bd3734aac82cd7 (patch)
tree72e0fb647722600449fb0675283e764df9da1d70 /lib/getgroups.c
parent08e6b4c6ba690adba619a3c1695bc312d3e98136 (diff)
downloadgnulib-80074d103cc72ea8f289b82c56bd3734aac82cd7.tar.gz
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 <ebb9@byu.net>
Diffstat (limited to 'lib/getgroups.c')
-rw-r--r--lib/getgroups.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/lib/getgroups.c b/lib/getgroups.c
index 207a441898..e764d732fc 100644
--- a/lib/getgroups.c
+++ b/lib/getgroups.c
@@ -20,20 +20,19 @@
#include <config.h>
-#undef getgroups
+#include <unistd.h>
-#include <stdio.h>
-#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
-#include <unistd.h>
-#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;