changeset 4827:a6d03da0fa67

Simplify the code by using new xalloc.h features.
author Paul Eggert <eggert@cs.ucla.edu>
date Thu, 30 Oct 2003 00:36:03 +0000
parents 441582dcfc37
children 8fcecc6d1619
files lib/ChangeLog lib/getusershell.c lib/hash.c lib/linebuffer.c
diffstat 4 files changed, 19 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,13 @@
 2003-10-29  Paul Eggert  <eggert@twinsun.com>
 
+	* getusershell.c (readname): Simplify the code by using x2nrealloc
+	rather than xmalloc/xrealloc.
+	* linebuffer.c (initbuffer, readlinebuffer): Simplify the code by
+	using x2realloc rather than xmalloc/xrealloc.  Also, fix a C
+	conformance bug: the old code used a pointer after freeing the
+	storage that it addressed.
+	* hash.c (hash_initialize): Simplify the code by using xalloc_oversized
+	rather than doing it by hand.
 	* getgroups.c (getgroups): Don't use xrealloc, since we don't need
 	the buffer preserved.  Use free and xmalloc instead.
 	* quotearg.c (quotearg_n_options): Likewise.
--- a/lib/getusershell.c
+++ b/lib/getusershell.c
@@ -145,29 +145,17 @@
   int c;
   size_t name_index = 0;
 
-  if (*name == NULL)
-    {
-      /* The initial size must be a power of two, so that the overflow
-	 check works.  */
-      *size = 16;
-
-      *name = xmalloc (*size);
-    }
-
   /* Skip blank space.  */
   while ((c = getc (stream)) != EOF && ISSPACE (c))
     /* Do nothing. */ ;
 
-  while (c != EOF && !ISSPACE (c))
+  for (;;)
     {
+      if (*size <= name_index)
+	*name = x2nrealloc (*name, size, sizeof **name);
+      if (c == EOF || ISSPACE (c))
+	break;
       (*name)[name_index++] = c;
-      if (*size < name_index)
-	{
-	  *size *= 2;
-	  if (! *size)
-	    xalloc_die ();
-	  *name = xrealloc (*name, *size);
-	}
       c = getc (stream);
     }
   (*name)[name_index] = '\0';
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -568,10 +568,10 @@
       candidate = new_candidate;
     }
 
-  if (SIZE_MAX / sizeof *table->bucket < candidate)
+  if (xalloc_oversized (candidate, sizeof *table->bucket))
     goto fail;
   table->n_buckets = next_prime (candidate);
-  if (SIZE_MAX / sizeof *table->bucket < table->n_buckets)
+  if (xalloc_oversized (table->n_buckets, sizeof *table->bucket))
     goto fail;
 
   table->bucket = calloc (table->n_buckets, sizeof *table->bucket);
--- a/lib/linebuffer.c
+++ b/lib/linebuffer.c
@@ -35,9 +35,7 @@
 void
 initbuffer (struct linebuffer *linebuffer)
 {
-  linebuffer->length = 0;
-  linebuffer->size = 200;
-  linebuffer->buffer = xmalloc (linebuffer->size);
+  memset (linebuffer, 0, sizeof *linebuffer);
 }
 
 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER.
@@ -73,9 +71,9 @@
 	}
       if (p == end)
 	{
-	  linebuffer->size *= 2;
-	  buffer = xrealloc (buffer, linebuffer->size);
-	  p = p - linebuffer->buffer + buffer;
+	  size_t oldsize = linebuffer->size;
+	  buffer = x2realloc (buffer, &linebuffer->size);
+	  p = buffer + oldsize;
 	  linebuffer->buffer = buffer;
 	  end = buffer + linebuffer->size;
 	}