fix return values of asprintf replacement.

This commit is contained in:
Oswald Buddenhagen 2004-03-29 22:32:40 +00:00
parent af8c4396fe
commit 2e8181a8d8
2 changed files with 14 additions and 18 deletions

View File

@ -65,14 +65,12 @@ vasprintf( char **strp, const char *fmt, va_list ap )
int len; int len;
char tmp[1024]; char tmp[1024];
if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap) ) < 0) if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap )) < 0 || !(*strp = malloc( len + 1 )))
*strp = 0; return -1;
else if ((*strp = malloc( len + 1 ))) { if (len >= (int)sizeof(tmp))
if (len >= sizeof(tmp))
vsprintf( *strp, fmt, ap ); vsprintf( *strp, fmt, ap );
else else
memcpy( *strp, tmp, len + 1 ); memcpy( *strp, tmp, len + 1 );
}
return len; return len;
} }
#endif #endif
@ -138,7 +136,7 @@ int
nfvasprintf( char **str, const char *fmt, va_list va ) nfvasprintf( char **str, const char *fmt, va_list va )
{ {
int ret = vasprintf( str, fmt, va ); int ret = vasprintf( str, fmt, va );
if (!*str) if (ret < 0)
oom(); oom();
return ret; return ret;
} }

View File

@ -166,14 +166,12 @@ vasprintf( char **strp, const char *fmt, va_list ap )
int len; int len;
char tmp[1024]; char tmp[1024];
if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap) ) < 0) if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap )) < 0 || !(*strp = malloc( len + 1 )))
*strp = 0; return -1;
else if ((*strp = malloc( len + 1 ))) { if (len >= (int)sizeof(tmp))
if (len >= sizeof(tmp))
vsprintf( *strp, fmt, ap ); vsprintf( *strp, fmt, ap );
else else
memcpy( *strp, tmp, len + 1 ); memcpy( *strp, tmp, len + 1 );
}
return len; return len;
} }
#endif #endif
@ -249,7 +247,7 @@ int
nfvasprintf( char **str, const char *fmt, va_list va ) nfvasprintf( char **str, const char *fmt, va_list va )
{ {
int ret = vasprintf( str, fmt, va ); int ret = vasprintf( str, fmt, va );
if (!*str) if (ret < 0)
oom(); oom();
return ret; return ret;
} }