Skip to content

Commit 961445a

Browse files
authored
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").
1 parent 04e09c3 commit 961445a

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

std/outbuffer.d

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -256,45 +256,24 @@ class OutBuffer
256256
auto psize = buffer.length;
257257
for (;;)
258258
{
259-
version(Windows)
259+
va_list args2;
260+
va_copy(args2, args);
261+
count = vsnprintf(p, psize, f, args2);
262+
va_end(args2);
263+
if (count == -1)
260264
{
261-
va_list args2;
262-
va_copy(args2, args);
263-
count = vsnprintf(p,psize,f,args2);
264-
va_end(args2);
265-
if (count != -1)
266-
break;
267-
268265
if (psize > psize.max / 2) assert(0); // overflow check
269266
psize *= 2;
270-
271-
p = cast(char *) alloca(psize); // buffer too small, try again with larger size
272267
}
273-
else version(Posix)
268+
else if (count >= psize)
274269
{
275-
va_list args2;
276-
va_copy(args2, args);
277-
count = vsnprintf(p, psize, f, args2);
278-
va_end(args2);
279-
if (count == -1)
280-
{
281-
if (psize > psize.max / 2) assert(0); // overflow check
282-
psize *= 2;
283-
}
284-
else if (count >= psize)
285-
{
286-
if (count == count.max) assert(0); // overflow check
287-
psize = count + 1;
288-
}
289-
else
290-
break;
291-
292-
p = cast(char *) alloca(psize); // buffer too small, try again with larger size
270+
if (count == count.max) assert(0); // overflow check
271+
psize = count + 1;
293272
}
294273
else
295-
{
296-
static assert(0);
297-
}
274+
break;
275+
276+
p = cast(char *) alloca(psize); // buffer too small, try again with larger size
298277
}
299278
write(cast(ubyte[]) p[0 .. count]);
300279
}

0 commit comments

Comments
 (0)