From 2e8181a8d80c1d0d04a2bc77bf732d6c0b310703 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Mon, 29 Mar 2004 22:32:40 +0000 Subject: [PATCH] fix return values of asprintf replacement. --- src/compat/util.c | 16 +++++++--------- src/util.c | 16 +++++++--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/compat/util.c b/src/compat/util.c index 9651ee5..358b0d3 100644 --- a/src/compat/util.c +++ b/src/compat/util.c @@ -65,14 +65,12 @@ vasprintf( char **strp, const char *fmt, va_list ap ) int len; char tmp[1024]; - if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap) ) < 0) - *strp = 0; - else if ((*strp = malloc( len + 1 ))) { - if (len >= sizeof(tmp)) - vsprintf( *strp, fmt, ap ); - else - memcpy( *strp, tmp, len + 1 ); - } + if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap )) < 0 || !(*strp = malloc( len + 1 ))) + return -1; + if (len >= (int)sizeof(tmp)) + vsprintf( *strp, fmt, ap ); + else + memcpy( *strp, tmp, len + 1 ); return len; } #endif @@ -138,7 +136,7 @@ int nfvasprintf( char **str, const char *fmt, va_list va ) { int ret = vasprintf( str, fmt, va ); - if (!*str) + if (ret < 0) oom(); return ret; } diff --git a/src/util.c b/src/util.c index 989a03f..6941eae 100644 --- a/src/util.c +++ b/src/util.c @@ -166,14 +166,12 @@ vasprintf( char **strp, const char *fmt, va_list ap ) int len; char tmp[1024]; - if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap) ) < 0) - *strp = 0; - else if ((*strp = malloc( len + 1 ))) { - if (len >= sizeof(tmp)) - vsprintf( *strp, fmt, ap ); - else - memcpy( *strp, tmp, len + 1 ); - } + if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap )) < 0 || !(*strp = malloc( len + 1 ))) + return -1; + if (len >= (int)sizeof(tmp)) + vsprintf( *strp, fmt, ap ); + else + memcpy( *strp, tmp, len + 1 ); return len; } #endif @@ -249,7 +247,7 @@ int nfvasprintf( char **str, const char *fmt, va_list va ) { int ret = vasprintf( str, fmt, va ); - if (!*str) + if (ret < 0) oom(); return ret; }