PR#331: for Visual Studio, use a snprintf/vsnprintf wrapper that ensures the string is terminated.

This commit is contained in:
Eric Haszlakiewicz
2017-08-25 01:15:39 -04:00
parent 256ebcd827
commit 81f6edbfd5
6 changed files with 41 additions and 18 deletions

36
snprintf_compat.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef __snprintf_compat_h
#define __snprintf_compat_h
/*
* Microsoft's _vsnprintf and _snprint don't always terminate
* the string, so use wrappers that ensure that.
*/
#include <stdarg.h>
#if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
static int json_c_vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
int ret;
ret = _vsnprintf(str, size, format, ap);
str[size - 1] = '\0';
return ret;
}
#define vsnprintf json_c_vsnprintf
static int json_c_snprintf(char *str, size_t size, const char *format, ...)
{
va_list ap;
int ret;
va_start(ap, format);
ret = json_c_vsnprintf(str, size, format, ap);
va_end(ap);
return ret;
}
#define snprintf json_c_snprintf
#elif !defined(HAVE_SNPRINTF) /* !HAVE_SNPRINTF */
# error Need vsnprintf!
#endif /* !HAVE_SNPRINTF && defined(WIN32) */
#endif /* __snprintf_compat_h */