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").
This commit is contained in:
kinke 2017-02-07 23:30:23 +01:00 committed by GitHub
parent 04e09c3ffa
commit 961445a303

View file

@ -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]);
}