aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2018-06-25 19:22:46 +0200
committerFlorian Weimer <fweimer@redhat.com>2018-06-25 19:47:59 +0200
commit1599ed4e959e6e5b319d82389667f51d01d53da0 (patch)
treef41415c4e8e8398ae9d41963ba2bf91823816b24
parenta26fe1638bf2fea58ddd44cc771896ad0c36de8c (diff)
downloadglibc-1599ed4e959e6e5b319d82389667f51d01d53da0.tar
glibc-1599ed4e959e6e5b319d82389667f51d01d53da0.tar.gz
glibc-1599ed4e959e6e5b319d82389667f51d01d53da0.tar.bz2
glibc-1599ed4e959e6e5b319d82389667f51d01d53da0.zip
getent: Use dynarray in initgroups_keys [BZ #18023]
-rw-r--r--ChangeLog6
-rw-r--r--nss/getent.c40
2 files changed, 36 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index a2fc4b793f..b015935195 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,12 @@
2018-06-25 Florian Weimer <fweimer@redhat.com>
[BZ #18023]
+ * nss/getent.c (initgroups_keys): Use dynarray instead of
+ extend_alloca.
+
+2018-06-25 Florian Weimer <fweimer@redhat.com>
+
+ [BZ #18023]
* nis/nss_nis/nis-initgroups.c (get_uid, _nss_nis_initgroups_dyn):
Use struct scratch_buffer instead of extend_alloca.
diff --git a/nss/getent.c b/nss/getent.c
index e609e5f9bb..92ade41d75 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>
@@ -473,34 +474,51 @@ netgroup_keys (int number, char *key[])
return result;
}
+#define DYNARRAY_STRUCT gid_list
+#define DYNARRAY_ELEMENT gid_t
+#define DYNARRAY_PREFIX gid_list_
+#define DYNARRAY_INITIAL_SIZE 10
+#include <malloc/dynarray-skeleton.c>
+
/* This is for initgroups */
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 gid_list list;
+ gid_list_init (&list);
+ if (!gid_list_resize (&list, 10))
+ {
+ fprintf (stderr, _("Could not allocate group list: %m\n"));
+ return 3;
+ }
+
for (int i = 0; i < number; ++i)
{
- int no = ngrps;
+ int no = gid_list_size (&list);
int n;
- while ((n = getgrouplist (key[i], -1, grps, &no)) == -1
- && no > ngrps)
+ while ((n = getgrouplist (key[i], -1, gid_list_begin (&list), &no)) == -1
+ && no > gid_list_size (&list))
{
- grps = extend_alloca (grps, grpslen, no * sizeof (gid_t));
- ngrps = no;
+ if (!gid_list_resize (&list, no))
+ {
+ fprintf (stderr, _("Could not allocate group list: %m\n"));
+ return 3;
+ }
}
if (n == -1)
- return 1;
+ {
+ gid_list_free (&list);
+ return 1;
+ }
+ const gid_t *grps = gid_list_begin (&list);
printf ("%-21s", key[i]);
for (int j = 0; j < n; ++j)
if (grps[j] != -1)
@@ -508,6 +526,8 @@ initgroups_keys (int number, char *key[])
putchar_unlocked ('\n');
}
+ gid_list_free (&list);
+
return 0;
}