Commit f443a72d334f8f52c3431e6b4181704a3f125d5e

Russell Belfer 2013-02-28T14:41:26

Fix some deprecation warnings on Windows This fixes some snprintf and vsnprintf related deprecation warnings we've been having on Windows with recent compilers.

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;