diff options
| author | Tim Kientzle <kientzle@FreeBSD.org> | 2004-04-13 23:49:02 +0000 |
|---|---|---|
| committer | Tim Kientzle <kientzle@FreeBSD.org> | 2004-04-13 23:49:02 +0000 |
| commit | 907d74bb26b00c3857d7ec2755d80c39b90455e4 (patch) | |
| tree | 5dbcbc43c3eb1b9aaf504fdb2d5012f9ed817369 /usr.bin | |
| parent | d911e48507b9aa5e39f200fb1e02e31a7593ce9a (diff) | |
Notes
Diffstat (limited to 'usr.bin')
| -rw-r--r-- | usr.bin/tar/util.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/usr.bin/tar/util.c b/usr.bin/tar/util.c index a9ccdfc4cb5d9..66e356dd1552d 100644 --- a/usr.bin/tar/util.c +++ b/usr.bin/tar/util.c @@ -45,19 +45,24 @@ void safe_fprintf(FILE *f, const char *fmt, ...) { char *buff; + char *buffheap; + char buffstack[256]; int bufflength; int length; va_list ap; char *p; - bufflength = 512; - buff = malloc(bufflength); + /* Use a stack-allocated buffer if we can. */ + buffheap = NULL; + bufflength = 256; + buff = buffstack; va_start(ap, fmt); length = vsnprintf(buff, bufflength, fmt, ap); + /* If the result is too large, allocate a buffer on the heap. */ if (length >= bufflength) { bufflength = length+1; - buff = realloc(buff, bufflength); + buff = buffheap = malloc(bufflength); length = vsnprintf(buff, bufflength, fmt, ap); } va_end(ap); @@ -83,7 +88,9 @@ safe_fprintf(FILE *f, const char *fmt, ...) fprintf(f, "\\%03o", c); } } - free(buff); + /* If we allocated a heap-based buffer, free it now. */ + if (buffheap != NULL) + free(buffheap); } static void |
