mojoshader_common.c
changeset 1153 663c9541e21f
parent 1150 02c0f0afb39a
child 1161 6085abb2edaa
--- 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)
     {