From 0c0be7e546a53f280dfedc98099cf82b6d316e8d Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 30 Oct 2003 00:36:03 +0000 Subject: [PATCH] Simplify the code by using new xalloc.h features. --- lib/ChangeLog | 8 ++++++++ lib/getusershell.c | 22 +++++----------------- lib/hash.c | 4 ++-- lib/linebuffer.c | 10 ++++------ 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index 429a189345..1325ed3336 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,13 @@ 2003-10-29 Paul Eggert + * 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. diff --git a/lib/getusershell.c b/lib/getusershell.c index c026ec0a9c..9b60f18d5b 100644 --- a/lib/getusershell.c +++ b/lib/getusershell.c @@ -145,29 +145,17 @@ readname (char **name, size_t *size, FILE *stream) 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'; diff --git a/lib/hash.c b/lib/hash.c index ad4599e4d5..19e593fef4 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -568,10 +568,10 @@ hash_initialize (size_t candidate, const Hash_tuning *tuning, 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); diff --git a/lib/linebuffer.c b/lib/linebuffer.c index bed5d94fb8..0d35fc0ce3 100644 --- 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 @@ readlinebuffer (struct linebuffer *linebuffer, FILE *stream) } 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; } -- 2.30.2