On Thursday 12 April 2007 00:47, Mike Frysinger wrote:
> in rev 16374, bb_get_chunk_from_file() was tweaked by adding another call to
> realloc() in case the buffer happened to be filled up completely ... however,
> this is done unconditionally which often leads to an extra realloc() call
> (whenever a line is not a multiple of 80 characters). this managed to expose
> a bug in uClibc's simple realloc() when the new size requested is actually
> smaller than the old size (people rarely use realloc() to *shrink* buffers,
> they use it to *expand* buffers). regardless, i'd propose this simple
> change:
> +if (!((idx+1) % 80))
> linebuf = xrealloc(linebuf, idx+1);
You are referring to this code:
char *bb_get_chunk_from_file(FILE * file, int *end)
{
int ch;
int idx = 0;
char *linebuf = NULL;
int linebufsz = 0;
while ((ch = getc(file)) != EOF) {
/* grow the line buffer as necessary */
if (idx >= linebufsz) {
linebuf = xrealloc(linebuf, linebufsz += 80);
}
linebuf[idx++] = (char) ch;
if (!ch || (end && ch == '\n'))
break;
}
if (end)
*end = idx;
if (linebuf) {
linebuf = xrealloc(linebuf, idx+1);
linebuf[idx] = '\0';
}
return linebuf;
}
Last realloc is used to truncate extra allocated data. Think about
very long file with short lines (for example, empty lines) being read
and stored in allocated memory using bb_get_chunk_from_file().
Without last realloc we may end up using lots more memory than really
needed. Doesn't sound like good idea to me. Maybe uclibc realloc()
should be fixed instead?
--
vda
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox