bug-gnulib
[Top][All Lists]
Advanced

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

[Bug-gnulib] exclude.c code cleanup to use xnrealloc


From: Paul Eggert
Subject: [Bug-gnulib] exclude.c code cleanup to use xnrealloc
Date: 15 Oct 2003 22:30:25 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

I installed this code-cleanup to exclude.c, so that it uses xnrealloc
instead of rolling its own overflow checking.

2003-10-15  Paul Eggert  <address@hidden>

        * exclude.c: Do not include <inttypes.h> or <stdint.h>.
        (SIZE_MAX): Remove.
        (new_exclude, add_exclude_file): Initial size no longer needs to
        be a power of 2.
        (add_exclude, add_exclude_file): Use xnrealloc instead of rolling
        our own address arithmetic overflow checking.

Index: lib/exclude.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/exclude.c,v
retrieving revision 1.18
diff -p -u -r1.18 exclude.c
--- lib/exclude.c       9 Sep 2003 03:15:05 -0000       1.18
+++ lib/exclude.c       16 Oct 2003 05:23:29 -0000
@@ -35,23 +35,12 @@ extern int errno;
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#if HAVE_INTTYPES_H
-# include <inttypes.h>
-#else
-# if HAVE_STDINT_H
-#  include <stdint.h>
-# endif
-#endif
 
 #include "exclude.h"
 #include "fnmatch.h"
 #include "unlocked-io.h"
 #include "xalloc.h"
 
-#ifndef SIZE_MAX
-# define SIZE_MAX ((size_t) -1)
-#endif
-
 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
 # define IN_CTYPE_DOMAIN(c) true
 #else
@@ -106,7 +95,7 @@ new_exclude (void)
 {
   struct exclude *ex = xmalloc (sizeof *ex);
   ex->exclude_count = 0;
-  ex->exclude_alloc = (1 << 6); /* This must be a power of 2.  */
+  ex->exclude_alloc = 60;
   ex->exclude = xmalloc (ex->exclude_alloc * sizeof ex->exclude[0]);
   return ex;
 }
@@ -201,11 +190,9 @@ add_exclude (struct exclude *ex, char co
 
   if (ex->exclude_alloc <= ex->exclude_count)
     {
-      size_t s = 2 * ex->exclude_alloc;
-      if (! (0 < s && s <= SIZE_MAX / sizeof ex->exclude[0]))
-       xalloc_die ();
-      ex->exclude_alloc = s;
-      ex->exclude = xrealloc (ex->exclude, s * sizeof ex->exclude[0]);
+      ex->exclude = xnrealloc (ex->exclude, ex->exclude_alloc,
+                              2 * sizeof *ex->exclude);
+      ex->exclude_alloc *= 2;
     }
 
   patopts = &ex->exclude[ex->exclude_count++];
@@ -229,7 +216,7 @@ add_exclude_file (void (*add_func) (stru
   char *p;
   char const *pattern;
   char const *lim;
-  size_t buf_alloc = (1 << 10);  /* This must be a power of two.  */
+  size_t buf_alloc = 1000;
   size_t buf_count = 0;
   int c;
   int e = 0;
@@ -246,10 +233,8 @@ add_exclude_file (void (*add_func) (stru
       buf[buf_count++] = c;
       if (buf_count == buf_alloc)
        {
+         buf = xnrealloc (buf, buf_alloc, 2);
          buf_alloc *= 2;
-         if (! buf_alloc)
-           xalloc_die ();
-         buf = xrealloc (buf, buf_alloc);
        }
     }
 




reply via email to

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