diff options
author | Florian Weimer <fweimer@redhat.com> | 2015-03-01 19:03:01 +0100 |
---|---|---|
committer | Florian Weimer <fweimer@redhat.com> | 2016-04-25 06:19:30 +0200 |
commit | 34ddffb50c2d2159c152cc34516f4c4088b3cc36 (patch) | |
tree | 05dcd2431e343c50634073864cf389ba4640dbe3 | |
parent | 4e10cf98c38e471f1c0637150c51fa2935b271fa (diff) | |
download | glibc-34ddffb50c2d2159c152cc34516f4c4088b3cc36.tar.gz |
getent: Switch to struct scratch_buffer in initgroups_keys
The retry loop is slightly different here because getgrouplist
provides size information, so scratch_buffer_set_array_size can be
used to grow the buffer in a more precise fashion.
[BZ #18023]
* nss/getent.c (initgroups_keys): Use struct scratch_buffer
instead of extend_alloca.
-rw-r--r-- | nss/getent.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/nss/getent.c b/nss/getent.c index de7b83ff38..e85d761744 100644 --- a/nss/getent.c +++ b/nss/getent.c @@ -39,6 +39,7 @@ #include <netinet/ether.h> #include <netinet/in.h> #include <sys/socket.h> +#include <scratch_buffer.h> /* Get libc version number. */ #include <version.h> @@ -477,30 +478,34 @@ netgroup_keys (int number, char *key[]) static int initgroups_keys (int number, char *key[]) { - int ngrps = 100; - size_t grpslen = ngrps * sizeof (gid_t); - gid_t *grps = alloca (grpslen); - if (number == 0) { fprintf (stderr, _("Enumeration not supported on %s\n"), "initgroups"); return 3; } + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); + for (int i = 0; i < number; ++i) { + ssize_t ngrps = tmpbuf.length / sizeof (gid_t); int no = ngrps; int n; - while ((n = getgrouplist (key[i], -1, grps, &no)) == -1 + while ((n = getgrouplist (key[i], -1, tmpbuf.data, &no)) == -1 && no > ngrps) { - grps = extend_alloca (grps, grpslen, no * sizeof (gid_t)); - ngrps = no; + if (!scratch_buffer_set_array_size (&tmpbuf, no, sizeof (gid_t))) + { + fprintf (stderr, _("Could not allocate group list: %m\n")); + return 3; + } } if (n == -1) return 1; + const gid_t *grps = tmpbuf.data; printf ("%-21s", key[i]); for (int j = 0; j < n; ++j) if (grps[j] != -1) @@ -508,6 +513,8 @@ initgroups_keys (int number, char *key[]) putchar_unlocked ('\n'); } + scratch_buffer_free (&tmpbuf); + return 0; } |