Deal with quirks of MSVC's vsnprintf() implementation.
authorBaldur Karlsson <baldur@unity3d.com>
Mon, 18 Apr 2016 12:06:17 -0400
changeset 1153 663c9541e21f
parent 1152 8f7653f0dc37
child 1154 1d6c2aadd16a
Deal with quirks of MSVC's vsnprintf() implementation.
mojoshader_common.c
--- a/mojoshader_common.c	Tue Feb 09 00:05:18 2016 -0500
+++ b/mojoshader_common.c	Mon Apr 18 12:06:17 2016 -0400
@@ -557,9 +557,24 @@
     char scratch[128];
     va_list ap;
     va_copy(ap, va);
-    const int len = vsnprintf(scratch, sizeof (scratch), fmt, ap);
+    int len = vsnprintf(scratch, sizeof (scratch), fmt, ap);
     va_end(ap);
 
+    // on some versions of the windows C runtime, vsnprintf() returns -1
+    // if the buffer overflows instead of the length the string would have
+    // been as expected.
+    // In this case we make another copy of va and fetch the length only
+    // with another call to _vscprintf
+
+#ifdef _MSC_VER
+    if (len == -1)
+    {
+        va_copy(ap, va);
+        len = _vscprintf(fmt, ap);
+        va_end(ap);
+    }
+#endif
+
     char *failstr = (char *) list->m(len + 1, list->d);
     if (failstr == NULL)
     {