Handle the case when MINGW32 vsnprintf returns -1

MINGW32 vsnprintf returns -1 if output is truncated.  Increased buffer
for vsnprintf in WinConsoleFile and fmt.
pull/20/head
Tatsuhiro Tsujikawa 2012-06-24 16:56:34 +09:00
parent 96720b297d
commit 73b75186c7
2 changed files with 18 additions and 4 deletions

View File

@ -74,12 +74,16 @@ size_t WinConsoleFile::write(const char* str)
int WinConsoleFile::printf(const char* format, ...)
{
char buf[1024];
char buf[2048];
va_list ap;
va_start(ap, format);
int r = vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
if(r <= 0) {
if(r == -1) {
// MINGW32 vsnprintf returns -1 if output is truncated.
r = strlen(buf);
} else if(r < 0) {
// Reachable?
return 0;
}
DWORD written;

View File

@ -44,10 +44,20 @@ std::string fmt(const char* fmtTemplate, ...)
{
va_list ap;
va_start(ap, fmtTemplate);
char buf[1024];
if(vsnprintf(buf, sizeof(buf), fmtTemplate, ap) <= 0) {
char buf[2048];
int rv;
rv = vsnprintf(buf, sizeof(buf), fmtTemplate, ap);
#ifdef __MINGW32__
// MINGW32 vsnprintf returns -1 if output is truncated.
if(rv < 0 && rv != -1) {
// Reachable?
buf[0] = '\0';
}
#else // !__MINGW32__
if(rv < 0) {
buf[0] = '\0';
}
#endif // !__MINGW32__
va_end(ap);
return buf;
}