From e2940f39b23f364a45832c94c94430fdf8a2c184 Mon Sep 17 00:00:00 2001 From: Baldur Karlsson Date: Mon, 18 Apr 2016 12:06:17 -0400 Subject: [PATCH] Deal with quirks of MSVC's vsnprintf() implementation. --- mojoshader_common.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/mojoshader_common.c b/mojoshader_common.c index a8b1cb8f..446d3897 100644 --- a/mojoshader_common.c +++ b/mojoshader_common.c @@ -557,9 +557,24 @@ int errorlist_add_va(ErrorList *list, const char *_fname, 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) {