1616#include <limits.h> /* for INT_MAX */
1717/* clang-format on */
1818
19+ #ifdef C89STRINGUTILS_TEST_MOCKS
20+ extern void * mock_malloc (size_t size );
21+ extern void * mock_realloc (void * ptr , size_t size );
22+ extern int mock_vsnprintf (char * str , size_t size , const char * format ,
23+ va_list ap );
24+ extern char * mock_strerror (int errnum );
25+ #define malloc mock_malloc
26+ #define realloc mock_realloc
27+ #define wtf_vsnprintf mock_vsnprintf
28+ #undef vsnprintf
29+ #define vsnprintf mock_vsnprintf
30+ #define strerror mock_strerror
31+ #endif
32+
1933#if defined(__GNUC__ ) && __GNUC__ >= 7 && !defined(__clang__ )
2034#pragma GCC diagnostic push
2135#pragma GCC diagnostic ignored "-Wnonnull-compare"
@@ -288,7 +302,6 @@ C89STRINGUTILS_EXPORT size_t c89stringutils_strerrorlen_s(errno_t errnum) {
288302 }
289303}
290304
291-
292305#ifndef VA_COPY
293306#if defined(HAVE_VA_COPY ) || defined(va_copy )
294307#define VA_COPY (dest , src ) va_copy(dest, src)
@@ -333,12 +346,17 @@ C89STRINGUTILS_EXPORT int c89stringutils_vasprintf(char **str, const char *fmt,
333346 VA_COPY (ap2 , ap );
334347 rc = vsnprintf (string , INIT_SZ , fmt , ap2 );
335348 va_end (ap2 );
336- if (rc >= 0 && rc < INIT_SZ ) { /* succeeded with initial alloc */
337- * str = string ;
338- } else if (rc == INT_MAX || rc < 0 ) { /* Bad length */
349+
350+ if (rc < 0 ) { /* Bad length */
339351 free (string );
340352 rc = -1 ;
341353 goto fail ;
354+ } else if (rc == INT_MAX ) { /* Bad length */
355+ free (string );
356+ rc = -1 ;
357+ goto fail ;
358+ } else if (rc < INIT_SZ ) { /* succeeded with initial alloc */
359+ * str = string ;
342360 } else { /* bigger than initial, realloc allowing for nul */
343361 len = (size_t )rc + 1 ;
344362 newstr = (char * )realloc (string , len );
@@ -350,7 +368,11 @@ C89STRINGUTILS_EXPORT int c89stringutils_vasprintf(char **str, const char *fmt,
350368 VA_COPY (ap2 , ap );
351369 rc = vsnprintf (newstr , len , fmt , ap2 );
352370 va_end (ap2 );
353- if (rc < 0 || (size_t )rc >= len ) { /* failed with realloc'ed string */
371+ if (rc < 0 ) { /* failed with realloc'ed string */
372+ free (newstr );
373+ rc = -1 ;
374+ goto fail ;
375+ } else if ((size_t )rc >= len ) {
354376 free (newstr );
355377 rc = -1 ;
356378 goto fail ;
@@ -360,23 +382,24 @@ C89STRINGUTILS_EXPORT int c89stringutils_vasprintf(char **str, const char *fmt,
360382 return rc ;
361383
362384fail :
363- if (rc != 0 ) {
364385#ifdef _MSC_VER
365- char errbuf [256 ];
366- int err_rc ;
367- err_rc = strerror_s (errbuf , sizeof (errbuf ), errno );
368- if (err_rc != 0 ) {
369- LOG_DEBUG ("strerror_s failed with rc=%d" , err_rc );
370- errbuf [0 ] = '\0' ;
371- }
372- LOG_DEBUG ("vasprintf failed with rc=%d, error=%s" , rc , errbuf );
386+ {
387+ char errbuf [256 ];
388+ int err_rc ;
389+ err_rc = strerror_s (errbuf , sizeof (errbuf ), errno );
390+ if (err_rc != 0 ) {
391+ LOG_DEBUG ("strerror_s failed with rc=%d" , err_rc );
392+ errbuf [0 ] = '\0' ;
393+ }
394+ LOG_DEBUG ("vasprintf failed with rc=%d, error=%s" , rc , errbuf );
395+ }
373396#else
374- const char * errstr ;
375- errstr = strerror (errno );
376- LOG_DEBUG ("vasprintf failed with rc=%d, error=%s" , rc ,
377- errstr ? errstr : "" );
397+ {
398+ const char * errstr ;
399+ errstr = strerror (errno );
400+ LOG_DEBUG ("vasprintf failed with rc=%d, error=%s" , rc , errstr ? errstr : "" );
401+ }
378402#endif
379- }
380403 * str = NULL ;
381404 errno = ENOMEM ;
382405 return -1 ;
@@ -490,7 +513,6 @@ C89STRINGUTILS_EXPORT int c89stringutils_jasprintf(char **unto, const char *fmt,
490513#pragma GCC diagnostic pop
491514#endif
492515
493-
494516#if !defined(HAVE_ASPRINTF )
495517C89STRINGUTILS_EXPORT int vasprintf (char * * str , const char * fmt , va_list ap ) {
496518 return c89stringutils_vasprintf (str , fmt , ap );
0 commit comments