diff options
-rw-r--r-- | lib/libc/stdio/asprintf.c | 35 | ||||
-rw-r--r-- | lib/libc/stdio/vasprintf.c | 36 |
2 files changed, 39 insertions, 32 deletions
diff --git a/lib/libc/stdio/asprintf.c b/lib/libc/stdio/asprintf.c index daca01a016d..fd0ba320c8f 100644 --- a/lib/libc/stdio/asprintf.c +++ b/lib/libc/stdio/asprintf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: asprintf.c,v 1.5 1998/08/14 21:39:39 deraadt Exp $ */ +/* $OpenBSD: asprintf.c,v 1.6 1998/10/16 16:11:55 millert Exp $ */ /* * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> @@ -28,7 +28,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: asprintf.c,v 1.5 1998/08/14 21:39:39 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: asprintf.c,v 1.6 1998/10/16 16:11:55 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -63,21 +63,26 @@ asprintf(str, fmt, va_alist) f._file = -1; f._flags = __SWR | __SSTR | __SALC; f._bf._base = f._p = (unsigned char *)malloc(128); - if (f._bf._base == NULL) { - *str = NULL; - errno = ENOMEM; - return (-1); - } - f._bf._size = f._w = 127; /* Leave room for the NULL */ + if (f._bf._base == NULL) + goto err; + f._bf._size = f._w = 127; /* Leave room for the NUL */ ret = vfprintf(&f, fmt, ap); + if (ret == -1) + goto err; *f._p = '\0'; va_end(ap); - _base = realloc(f._bf._base, f._bf._size + 1); - if (_base == NULL) { - errno = ENOMEM; - ret = -1; - } - f._bf._base = _base; - *str = (char *)f._bf._base; + _base = realloc(f._bf._base, ret + 1); + if (_base == NULL) + goto err; + *str = (char *)_base; return (ret); + +err: + if (f._bf._base) { + free(f._bf._base); + f._bf._base = NULL; + } + *str = NULL; + errno = ENOMEM; + return (-1); } diff --git a/lib/libc/stdio/vasprintf.c b/lib/libc/stdio/vasprintf.c index fc3b2c95e98..e2fec8d6d9a 100644 --- a/lib/libc/stdio/vasprintf.c +++ b/lib/libc/stdio/vasprintf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vasprintf.c,v 1.5 1998/08/14 21:39:41 deraadt Exp $ */ +/* $OpenBSD: vasprintf.c,v 1.6 1998/10/16 16:11:56 millert Exp $ */ /* * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> @@ -28,7 +28,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: vasprintf.c,v 1.5 1998/08/14 21:39:41 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: vasprintf.c,v 1.6 1998/10/16 16:11:56 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -48,23 +48,25 @@ vasprintf(str, fmt, ap) f._file = -1; f._flags = __SWR | __SSTR | __SALC; f._bf._base = f._p = (unsigned char *)malloc(128); - if (f._bf._base == NULL) { - *str = NULL; - errno = ENOMEM; - return (-1); - } - f._bf._size = f._w = 127; /* Leave room for the NULL */ + if (f._bf._base == NULL) + goto err; + f._bf._size = f._w = 127; /* Leave room for the NUL */ ret = vfprintf(&f, fmt, ap); + if (ret == -1) + goto err; *f._p = '\0'; - _base = realloc(f._bf._base, f._bf._size + 1); - if (_base == NULL) { - if (f._bf._base) - free(f._bf._base); + _base = realloc(f._bf._base, ret + 1); + if (_base == NULL) + goto err; + *str = (char *)_base; + return (ret); + +err: + if (f._bf._base) { + free(f._bf._base); f._bf._base = NULL; - errno = ENOMEM; - ret = -1; } - f._bf._base = _base; - *str = (char *)f._bf._base; - return (ret); + *str = NULL; + errno = ENOMEM; + return (-1); } |