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.
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';
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);
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.
}
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;
}