From b9ff0728e1bab018dd888c173151dba35e40ef3b Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Wed, 30 Jul 2014 06:53:02 -0500 Subject: harmonize asprintf with OpenSSH * use the original name for the file from OpenSSH (remove duplicate version) * add va_copy/__va_copy checks to configure * incorporate proposed fixes to openssh version: + include more system headers directly for various definitions + limit the scope of va_copy/va_end to their affected vsnprintf calls + simplify error handling, removing a dead assignment --- crypto/compat/bsd-asprintf.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'crypto/compat/bsd-asprintf.c') diff --git a/crypto/compat/bsd-asprintf.c b/crypto/compat/bsd-asprintf.c index 3368195..8ccfa22 100644 --- a/crypto/compat/bsd-asprintf.c +++ b/crypto/compat/bsd-asprintf.c @@ -17,12 +17,12 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "includes.h" - #ifndef HAVE_VASPRINTF #include +#include /* for INT_MAX */ #include +#include /* for vsnprintf */ #include #ifndef VA_COPY @@ -42,16 +42,17 @@ int vasprintf(char **str, const char *fmt, va_list ap) { - int ret = -1; + int ret; va_list ap2; char *string, *newstr; size_t len; - VA_COPY(ap2, ap); if ((string = malloc(INIT_SZ)) == NULL) goto fail; + VA_COPY(ap2, ap); ret = vsnprintf(string, INIT_SZ, fmt, ap2); + va_end(ap2); if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */ *str = string; } else if (ret == INT_MAX || ret < 0) { /* Bad length */ @@ -62,25 +63,21 @@ vasprintf(char **str, const char *fmt, va_list ap) if ((newstr = realloc(string, len)) == NULL) { free(string); goto fail; - } else { - va_end(ap2); - VA_COPY(ap2, ap); - ret = vsnprintf(newstr, len, fmt, ap2); - if (ret >= 0 && (size_t)ret < len) { - *str = newstr; - } else { /* failed with realloc'ed string, give up */ - free(newstr); - goto fail; - } } + VA_COPY(ap2, ap); + ret = vsnprintf(newstr, len, fmt, ap2); + va_end(ap2); + if (ret < 0 || (size_t)ret >= len) { /* failed with realloc'ed string */ + free(newstr); + goto fail; + } + *str = newstr; } - va_end(ap2); return (ret); fail: *str = NULL; errno = ENOMEM; - va_end(ap2); return (-1); } #endif -- cgit v1.2.3-55-g6feb