1111#include <stdio.h>
1212#include <stdlib.h>
1313#include <string.h>
14+ #include <errno.h>
15+ #include <limits.h> /* for INT_MAX */
1416/* clang-format on */
1517
1618#ifndef HAVE_SNPRINTF_H
@@ -37,6 +39,9 @@ static int wtf_snprintf(char *buffer, size_t count, const char *format, ...) {
3739 va_start (args , format );
3840#if defined(_MSC_VER )
3941 rc = _vsnprintf_s (buffer , count , _TRUNCATE , format , args );
42+ if (rc < 0 ) {
43+ rc = _vscprintf (format , args );
44+ }
4045#else
4146 rc = _vsnprintf (buffer , count , format , args );
4247#endif
@@ -53,6 +58,9 @@ static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
5358 int rc ;
5459#if defined(_MSC_VER )
5560 rc = _vsnprintf_s (buffer , count , _TRUNCATE , format , args );
61+ if (rc < 0 ) {
62+ rc = _vscprintf (format , args );
63+ }
5664#else
5765 rc = _vsnprintf (buffer , count , format , args );
5866#endif
@@ -184,10 +192,6 @@ size_t strerrorlen_s(errno_t errnum) {
184192#ifndef HAVE_ASPRINTF
185193#define HAVE_ASPRINTF
186194
187- #include <errno.h>
188- #include <limits.h> /* for INT_MAX */
189- #include <stdlib.h>
190-
191195#ifndef VA_COPY
192196#if defined(HAVE_VA_COPY ) || defined(va_copy )
193197#define VA_COPY (dest , src ) va_copy(dest, src)
@@ -286,6 +290,7 @@ char *jasprintf(char **unto, const char *fmt, ...) {
286290 va_list args ;
287291 size_t base_length ;
288292 int length ;
293+ int rc ;
289294 char * result ;
290295
291296 base_length = unto && * unto ? strlen (* unto ) : 0 ;
@@ -299,16 +304,29 @@ char *jasprintf(char **unto, const char *fmt, ...) {
299304#endif
300305 va_end (args );
301306
307+ if (length < 0 )
308+ return NULL ;
309+
302310 /* check result for failure */
303311 result =
304312 (char * )realloc (unto ? * unto : NULL , base_length + (size_t )length + 1 );
305313
314+ if (result == NULL )
315+ return NULL ;
316+
306317 va_start (args , fmt );
307318 /* check for failure*/
308319#if defined(_MSC_VER )
309- vsprintf_s (result + base_length , (size_t )length + 1 , fmt , args );
320+ rc = vsprintf_s (result + base_length , (size_t )length + 1 , fmt , args );
321+ if (rc < 0 ) {
322+ /* handle error, printing the nonzero exit code for debug purposes */
323+ LOG_DEBUG ("vsprintf_s failed with rc=%d\n" , rc );
324+ }
310325#else
311- vsprintf (result + base_length , fmt , args );
326+ rc = vsprintf (result + base_length , fmt , args );
327+ if (rc < 0 ) {
328+ LOG_DEBUG ("vsprintf failed with rc=%d\n" , rc );
329+ }
312330#endif
313331 va_end (args );
314332
0 commit comments