]> granicus.if.org Git - json-c/commitdiff
PR#331: for Visual Studio, use a snprintf/vsnprintf wrapper that ensures the string...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Fri, 25 Aug 2017 05:15:39 +0000 (01:15 -0400)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Fri, 25 Aug 2017 05:15:39 +0000 (01:15 -0400)
json_object.c
json_util.c
printbuf.c
snprintf_compat.h [new file with mode: 0644]
tests/test_locale.c
vasprintf_compat.h

index 7148731bce56c7de8b05604b8df5ae71b75b757b..a3171230fc9f57af3aa913f0b534b282a3ad6e21 100644 (file)
 #include "json_util.h"
 #include "math_compat.h"
 #include "strdup_compat.h"
-
-#if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
-  /* MSC has the version as _snprintf */
-# define snprintf _snprintf
-#elif !defined(HAVE_SNPRINTF)
-# error You do not have snprintf on your system.
-#endif /* HAVE_SNPRINTF */
+#include "snprintf_compat.h"
 
 // Don't define this.  It's not thread-safe.
 /* #define REFCOUNT_DEBUG 1 */
index cc57e34f7338c47e7e754e5ca4eb9c310d407806..2121150a02edb3f400ef3c7bedc1494b6e3f4513 100644 (file)
 # define open _open
 #endif
 
-#if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
-  /* MSC has the version as _snprintf */
-# define snprintf _snprintf
-#elif !defined(HAVE_SNPRINTF)
-# error You do not have snprintf on your system.
-#endif /* HAVE_SNPRINTF */
+#include "snprintf_compat.h"
 
 #include "debug.h"
 #include "printbuf.h"
index 2745339c3ce091b8afab76624c8a96a567697fc5..6c77b5defdcd7724607278b857d6081701ab3549 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "debug.h"
 #include "printbuf.h"
+#include "snprintf_compat.h"
 #include "vasprintf_compat.h"
 
 static int printbuf_extend(struct printbuf *p, int min_size);
diff --git a/snprintf_compat.h b/snprintf_compat.h
new file mode 100644 (file)
index 0000000..7323aaa
--- /dev/null
@@ -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 */
index a3e319da1bfcd19fe2c6a0b5f48572e59629f865..7d6541ae93162f2c433cff2dad1db8d996966559 100644 (file)
@@ -7,6 +7,7 @@
 #include "config.h"
 #include "json.h"
 #include "json_tokener.h"
+#include "snprintf_compat.h"
 
 #ifdef HAVE_LOCALE_H
 #include <locale.h>
index 51e234b360b77be77564d448d46ddc6f1fcf280d..40ff550f881e8a97907ee254cdd71af25fe2cbcb 100644 (file)
@@ -1,11 +1,7 @@
 #ifndef __vasprintf_compat_h
 #define __vasprintf_compat_h
 
-#if !defined(HAVE_VSNPRINTF) && defined(_MSC_VER)
-# define vsnprintf _vsnprintf
-#elif !defined(HAVE_VSNPRINTF) /* !HAVE_VSNPRINTF */
-# error Need vsnprintf!
-#endif /* !HAVE_VSNPRINTF && defined(WIN32) */
+#include "snprintf_compat.h"
 
 #if !defined(HAVE_VASPRINTF)
 /* CAW: compliant version of vasprintf */