From 34ddffb50c2d2159c152cc34516f4c4088b3cc36 Mon Sep 17 00:00:00 2001 From: Florian Weimer Date: Sun, 1 Mar 2015 19:03:01 +0100 Subject: 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. --- nss/getent.c | 21 ++++++++++++++------- 1 file 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 #include #include +#include /* Get libc version number. */ #include @@ -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; } -- cgit v1.2.1