summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authortjr <tjr@FreeBSD.org>2002-09-26 13:11:24 +0000
committertjr <tjr@FreeBSD.org>2002-09-26 13:11:24 +0000
commit18a73315b6b0c89155e8f2db0dabc48b27367e78 (patch)
tree4de1e26fefecf761f67eb239ab65759349d165e6 /lib
parent7733fef36f707ac0914a4c0b1cc2958accbeffb0 (diff)
downloadFreeBSD-src-18a73315b6b0c89155e8f2db0dabc48b27367e78.zip
FreeBSD-src-18a73315b6b0c89155e8f2db0dabc48b27367e78.tar.gz
Back out previous, free the buffer when __vfprintf() fails and don't bother
trying to shrink the buffer with realloc() before returning it.
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdio/vasprintf.c32
1 files changed, 13 insertions, 19 deletions
diff --git a/lib/libc/stdio/vasprintf.c b/lib/libc/stdio/vasprintf.c
index 2d2f9e0..01ac068 100644
--- a/lib/libc/stdio/vasprintf.c
+++ b/lib/libc/stdio/vasprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vasprintf.c,v 1.6 1998/10/16 16:11:56 millert Exp $ */
+/* $OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -44,32 +44,26 @@ vasprintf(str, fmt, ap)
int ret;
FILE f;
struct __sFILEX ext;
- unsigned char *_base;
f._file = -1;
f._flags = __SWR | __SSTR | __SALC;
f._bf._base = f._p = (unsigned char *)malloc(128);
- if (f._bf._base == NULL)
- goto err;
+ if (f._bf._base == NULL) {
+ *str = NULL;
+ errno = ENOMEM;
+ return (-1);
+ }
f._bf._size = f._w = 127; /* Leave room for the NUL */
f._extra = &ext;
INITEXTRA(&f);
ret = __vfprintf(&f, fmt, ap);
- if (ret == -1)
- goto err;
- *f._p = '\0';
- _base = realloc(f._bf._base, ret + 1);
- if (_base == NULL)
- goto err;
- *str = (char *)_base;
- return (ret);
-
-err:
- if (f._bf._base != NULL) {
+ if (ret < 0) {
free(f._bf._base);
- f._bf._base = NULL;
+ *str = NULL;
+ errno = ENOMEM;
+ return (-1);
}
- *str = NULL;
- errno = ENOMEM;
- return (-1);
+ *f._p = '\0';
+ *str = (char *)f._bf._base;
+ return (ret);
}
OpenPOWER on IntegriCloud