# HG changeset patch # User Paul Eggert # Date 1067474163 0 # Node ID a6d03da0fa67f6a476ee765c2fb60d6936fb67d3 # Parent 441582dcfc376423efd74a7490a166b2f34382f7 Simplify the code by using new xalloc.h features. diff --git a/lib/ChangeLog b/lib/ChangeLog --- 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 --- 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'; diff --git a/lib/hash.c b/lib/hash.c --- 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); diff --git a/lib/linebuffer.c b/lib/linebuffer.c --- 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; }