bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 08/13] getgroups: use walloc


From: Paul Eggert
Subject: [PATCH 08/13] getgroups: use walloc
Date: Sun, 4 Jun 2017 23:45:58 -0700

* lib/getgroups.c: Include minmax.h, walloc.h, limits.h.
(rpl_getgroups): Use walloc.h functions instead of checking for
overflow by hand.
* modules/getgroups (Depends-on): Add minmax, walloc.
Remove malloc-posix.
---
 ChangeLog         |  7 +++++++
 lib/getgroups.c   | 24 ++++++++++--------------
 modules/getgroups |  3 ++-
 3 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e3be3b3..4ced38a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2017-06-04  Paul Eggert  <address@hidden>
 
+       getgroups: use walloc
+       * lib/getgroups.c: Include minmax.h, walloc.h, limits.h.
+       (rpl_getgroups): Use walloc.h functions instead of checking for
+       overflow by hand.
+       * modules/getgroups (Depends-on): Add minmax, walloc.
+       Remove malloc-posix.
+
        exclude: use xwalloc instead of xalloc
        * lib/exclude.c: Include xwalloc.h instead of walloc.h.
        (struct exclude_pattern, exclude_add_pattern_buffer):
diff --git a/lib/getgroups.c b/lib/getgroups.c
index dce0f2d..0ac303e 100644
--- a/lib/getgroups.c
+++ b/lib/getgroups.c
@@ -21,7 +21,11 @@
 
 #include <unistd.h>
 
+#include "minmax.h"
+#include "walloc.h"
+
 #include <errno.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <stdint.h>
 
@@ -84,12 +88,7 @@ rpl_getgroups (int n, gid_t *group)
       if (sizeof *group == sizeof *gbuf)
         return getgroups (n, (GETGROUPS_T *) group);
 
-      if (SIZE_MAX / sizeof *gbuf <= n)
-        {
-          errno = ENOMEM;
-          return -1;
-        }
-      gbuf = malloc (n * sizeof *gbuf);
+      gbuf = wreallocarray (NULL, n, sizeof *gbuf);
       if (!gbuf)
         return -1;
       result = getgroups (n, gbuf);
@@ -105,20 +104,17 @@ rpl_getgroups (int n, gid_t *group)
       return result;
     }
 
-  n = 20;
+  ptrdiff_t nalloc = 13;
   while (1)
     {
-      /* 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 = malloc (n * sizeof *gbuf);
+      int nalloc_max = MIN (INT_MAX, PTRDIFF_MAX);
+      gbuf = wgrowalloc (NULL, &nalloc, 1, nalloc_max, sizeof *gbuf);
       if (!gbuf)
         return -1;
-      n_groups = getgroups (n, gbuf);
-      if (n_groups == -1 ? errno != EINVAL : n_groups < n)
+      n_groups = getgroups (nalloc, gbuf);
+      if (n_groups == -1 ? errno != EINVAL : n_groups < nalloc)
         break;
       free (gbuf);
-      n *= 2;
     }
 
   saved_errno = errno;
diff --git a/modules/getgroups b/modules/getgroups
index 9926a12..527f35d 100644
--- a/modules/getgroups
+++ b/modules/getgroups
@@ -7,8 +7,9 @@ m4/getgroups.m4
 
 Depends-on:
 unistd
-malloc-posix    [test $HAVE_GETGROUPS = 0 || test $REPLACE_GETGROUPS = 1]
+minmax          [test $HAVE_GETGROUPS = 0 || test $REPLACE_GETGROUPS = 1]
 stdint          [test $HAVE_GETGROUPS = 0 || test $REPLACE_GETGROUPS = 1]
+walloc          [test $HAVE_GETGROUPS = 0 || test $REPLACE_GETGROUPS = 1]
 
 configure.ac:
 gl_FUNC_GETGROUPS
-- 
2.9.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]