bug-gnulib
[Top][All Lists]
Advanced

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

[Bug-gnulib] getusershell, linebuffer, hash changes to use new xalloc.h


From: Paul Eggert
Subject: [Bug-gnulib] getusershell, linebuffer, hash changes to use new xalloc.h
Date: 29 Oct 2003 16:40:43 -0800
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

I installed the following.  Mostly it's just code simplification, but
it does fix one (largely theoretical) conformance bug in linebuffer.c.
The old code used a nondereferenced pointer into freed storage.

There should be more changes coming soon.

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

        * 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.

Index: lib/getusershell.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getusershell.c,v
retrieving revision 1.17
diff -p -u -r1.17 getusershell.c
--- lib/getusershell.c  9 Sep 2003 19:26:25 -0000       1.17
+++ lib/getusershell.c  30 Oct 2003 00:35:09 -0000
@@ -145,29 +145,17 @@ readname (char **name, size_t *size, FIL
   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';
Index: lib/linebuffer.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/linebuffer.c,v
retrieving revision 1.16
diff -p -u -r1.16 linebuffer.c
--- lib/linebuffer.c    24 Sep 2003 20:56:42 -0000      1.16
+++ lib/linebuffer.c    30 Oct 2003 00:35:10 -0000
@@ -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 @@ readlinebuffer (struct linebuffer *lineb
        }
       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;
        }
Index: lib/hash.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/hash.c,v
retrieving revision 1.34
diff -p -u -r1.34 hash.c
--- lib/hash.c  26 Oct 2003 00:14:40 -0000      1.34
+++ lib/hash.c  30 Oct 2003 00:35:11 -0000
@@ -568,10 +568,10 @@ hash_initialize (size_t candidate, const
       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);




reply via email to

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