summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWink Saville <wink@google.com>2010-11-20 07:22:47 -0800
committerWink Saville <wink@google.com>2010-11-20 07:22:47 -0800
commit2b77a5377db63d95d777a7d5aa14380dd0e71e72 (patch)
tree754ea65f8923e8fcc51f7c075b7c5d59ec31a6f7
parent1fddfb0177911d1a1f9e13904434a6f30f9cecb0 (diff)
downloadping-2b77a5377db63d95d777a7d5aa14380dd0e71e72.tar.gz
Dynamically allocate groups array dynamically based on its actual size.
Change-Id: Ibd6692c2396d793756e07fc080423acd2ecc1af0
-rw-r--r--ping.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/ping.c b/ping.c
index 80d9663..daefc41 100644
--- a/ping.c
+++ b/ping.c
@@ -1673,6 +1673,7 @@ void usage(void)
int isInSupplementaryGroup(gid_t group) {
long ngroups_max;
+ gid_t empty[0];
gid_t *groups;
int ngroups;
int i;
@@ -1682,13 +1683,17 @@ int isInSupplementaryGroup(gid_t group) {
return 1;
}
- ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
- groups = (gid_t *) malloc(ngroups_max * sizeof(gid_t));
+ ngroups = getgroups(0, empty);
+ if (ngroups < 0) {
+ perror("ping: call to getgroups for sizing failed");
+ exit(2);
+ }
+ groups = (gid_t *) malloc((ngroups * sizeof(gid_t)));
if (groups == NULL) {
- fprintf(stderr, "ping: unable to allocate memory. Aborting\n");
+ fprintf(stderr, "ping: unable to allocate memory for %d groups. Aborting\n", ngroups);
exit(2);
}
- ngroups = getgroups(ngroups_max, groups);
+ ngroups = getgroups(ngroups, groups);
if (ngroups < 0) {
perror("ping: getgroups failed");
exit(2);