Changeset: 0813ac0444a3 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=0813ac0444a3
Modified Files:
        NT/monetdb_config.h.in
Branch: Jul2015
Log Message:

Windows _snprintf doesn't null-terminate when buffer too small.
Also, it returns -1.  We work around these shortcomings.


diffs (80 lines):

diff --git a/NT/monetdb_config.h.in b/NT/monetdb_config.h.in
--- a/NT/monetdb_config.h.in
+++ b/NT/monetdb_config.h.in
@@ -49,6 +49,14 @@
 #include <stddef.h>
 #include <ws2tcpip.h>
 
+#include <sys/types.h>
+#include <stdio.h>             /* NULL, printf etc. */
+#include <stdlib.h>
+#include <errno.h>
+#include <stdarg.h>            /* va_alist.. */
+
+#include <assert.h>
+
 /* indicate to sqltypes.h that windows.h has already been included and
    that it doesn't have to define Windows constants */
 #define ALREADY_HAVE_WINDOWS_TYPE 1
@@ -995,9 +1003,37 @@
 /* #undef size_t */
 
 #if _MSC_VER < 1900
-#ifndef snprintf
-#define snprintf _snprintf
-#endif
+#define snprintf c99_snprintf
+#define vsnprintf c99_vsnprintf
+
+/* Microsoft _snprintf returns -1 and does not null-terminate when the
+ * buffer is too small, so work around that */
+
+static inline int
+c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap)
+{
+    int count = -1;
+
+    if (size != 0)
+        count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
+    if (count == -1)
+        count = _vscprintf(format, ap);
+
+    return count;
+}
+
+static inline int
+c99_snprintf(char *outBuf, size_t size, const char *format, ...)
+{
+    int count;
+    va_list ap;
+
+    va_start(ap, format);
+    count = c99_vsnprintf(outBuf, size, format, ap);
+    va_end(ap);
+
+    return count;
+}
 #endif
 
 /* type used by connect */
@@ -1020,20 +1056,6 @@ typedef unsigned short uint16_t;
 typedef unsigned int uint32_t;
 typedef unsigned __int64 uint64_t;
 
-#if _MSC_VER < 1500
-#ifndef vsnprintf
-#define vsnprintf _vsnprintf
-#endif
-#endif
-
-#include <sys/types.h>
-#include <stdio.h>             /* NULL, printf etc. */
-#include <stdlib.h>
-#include <errno.h>
-#include <stdarg.h>            /* va_alist.. */
-
-#include <assert.h>
-
 /* normally defined in stdbool.h, but that doesn't exist on Windows */
 #define true 1
 #define false 0
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to