changeset 15783:4b59b93e3c12

strerror_r-posix: Fix for MSVC 9. * lib/strerror_r.c (local_snprintf): New function. (snprintf): Define to local_snprintf, not to _snprintf.
author Bruno Haible <bruno@clisp.org>
date Sun, 25 Sep 2011 14:54:47 +0200
parents 402bb52d5f11
children f1e8251b45f3
files ChangeLog lib/strerror_r.c
diffstat 2 files changed, 23 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2011-09-25  Bruno Haible  <bruno@clisp.org>
+
+	strerror_r-posix: Fix for MSVC 9.
+	* lib/strerror_r.c (local_snprintf): New function.
+	(snprintf): Define to local_snprintf, not to _snprintf.
+
 2011-09-25  Bruno Haible  <bruno@clisp.org>
 
 	ftruncate: Support for MSVC 9.
--- a/lib/strerror_r.c
+++ b/lib/strerror_r.c
@@ -87,9 +87,24 @@
 #endif
 
 /* On MSVC, there is no snprintf() function, just a _snprintf().
-   It is of lower quality, but sufficient for the simple use here.  */
+   It is of lower quality, but sufficient for the simple use here.
+   We only have to make sure to NUL terminate the result (_snprintf
+   does not NUL terminate, like strncpy).  */
 #if !HAVE_SNPRINTF
-# define snprintf _snprintf
+static int
+local_snprintf (char *buf, size_t buflen, const char *format, ...)
+{
+  va_list args;
+  int result;
+
+  va_start (args, format);
+  result = _vsnprintf (buf, buflen, format, args);
+  va_end (args);
+  if (buflen > 0 && (result < 0 || result >= buflen))
+    buf[buflen - 1] = '\0';
+  return result;
+}
+# define snprintf local_snprintf
 #endif
 
 /* Copy as much of MSG into BUF as possible, without corrupting errno.