From 961445a30360ec4f041161e8ae9efb8eed3dba84 Mon Sep 17 00:00:00 2001 From: kinke Date: Tue, 7 Feb 2017 23:30:23 +0100 Subject: [PATCH] Fix std.outbuffer.[v]printf() for Visual Studio 2015+ MS conforms to the standard beginning with VS 2015, i.e., if the buffer is too small, vsnprintf() returns the number of required characters (excl. terminating null). VS 2013 and earlier always returned -1 in that case. So just use the generic (previous POSIX) version, it's compatible with older VS anyway. Already unittested in line 380 ("hello world 6" vs. "hello world 62665"). --- std/outbuffer.d | 43 +++++++++++-------------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/std/outbuffer.d b/std/outbuffer.d index 99dbb4869..3ce4bcf9d 100644 --- a/std/outbuffer.d +++ b/std/outbuffer.d @@ -256,45 +256,24 @@ class OutBuffer auto psize = buffer.length; for (;;) { - version(Windows) + va_list args2; + va_copy(args2, args); + count = vsnprintf(p, psize, f, args2); + va_end(args2); + if (count == -1) { - va_list args2; - va_copy(args2, args); - count = vsnprintf(p,psize,f,args2); - va_end(args2); - if (count != -1) - break; - if (psize > psize.max / 2) assert(0); // overflow check psize *= 2; - - p = cast(char *) alloca(psize); // buffer too small, try again with larger size } - else version(Posix) + else if (count >= psize) { - va_list args2; - va_copy(args2, args); - count = vsnprintf(p, psize, f, args2); - va_end(args2); - if (count == -1) - { - if (psize > psize.max / 2) assert(0); // overflow check - psize *= 2; - } - else if (count >= psize) - { - if (count == count.max) assert(0); // overflow check - psize = count + 1; - } - else - break; - - p = cast(char *) alloca(psize); // buffer too small, try again with larger size + if (count == count.max) assert(0); // overflow check + psize = count + 1; } else - { - static assert(0); - } + break; + + p = cast(char *) alloca(psize); // buffer too small, try again with larger size } write(cast(ubyte[]) p[0 .. count]); }