Fix some deprecation warnings on Windows This fixes some snprintf and vsnprintf related deprecation warnings we've been having on Windows with recent compilers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
diff --git a/src/common.h b/src/common.h
index 48c4b54..e3a9e19 100644
--- a/src/common.h
+++ b/src/common.h
@@ -33,11 +33,9 @@
# include "win32/pthread.h"
#endif
-# define snprintf _snprintf
-
#else
-# include <unistd.h>
+# include <unistd.h>
# ifdef GIT_THREADS
# include <pthread.h>
# endif
diff --git a/src/win32/msvc-compat.h b/src/win32/msvc-compat.h
index df2111d..50865ed 100644
--- a/src/win32/msvc-compat.h
+++ b/src/win32/msvc-compat.h
@@ -37,6 +37,13 @@
/* MSVC doesn't define ssize_t at all */
typedef SSIZE_T ssize_t;
+/* define snprintf using variadic macro support if available */
+#if _MSC_VER >= 1400
+# define snprintf(BUF, SZ, FMT, ...) _snprintf_s(BUF, SZ, _TRUNCATE, FMT, __VA_ARGS__)
+#else
+# define snprintf _snprintf
+#endif
+
#endif
#define GIT_STDLIB_CALL __cdecl
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index f533eaa..83b11ff 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -375,7 +375,8 @@ int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr)
#ifdef _MSC_VER
int len;
- if (count == 0 || (len = _vsnprintf(buffer, count, format, argptr)) < 0)
+ if (count == 0 ||
+ (len = _vsnprintf_s(buffer, count, _TRUNCATE, format, argptr)) < 0)
return _vscprintf(format, argptr);
return len;