From: Roger Meier Date: Tue, 4 Jun 2013 20:25:06 +0000 (+0200) Subject: THRIFT-1982 vsnprintf on Windows have different semantics X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=d65216df190b0ff1522098c8a552594ce29feb3d;p=common%2Fthrift.git THRIFT-1982 vsnprintf on Windows have different semantics Patch: Konrad Grochowski --- diff --git a/lib/cpp/src/thrift/Thrift.cpp b/lib/cpp/src/thrift/Thrift.cpp index bcbdb1a4..6c7f8aee 100644 --- a/lib/cpp/src/thrift/Thrift.cpp +++ b/lib/cpp/src/thrift/Thrift.cpp @@ -34,6 +34,19 @@ void TOutput::printf(const char *message, ...) { char stack_buf[STACK_BUF_SIZE]; va_list ap; +#ifdef _MSC_VER + va_start(ap, message); + int need = _vscprintf(message, ap); + va_end(ap); + + if (need < STACK_BUF_SIZE) { + va_start(ap, message); + vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap); + va_end(ap); + f_(stack_buf); + return; + } +#else va_start(ap, message); int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap); va_end(ap); @@ -42,9 +55,15 @@ void TOutput::printf(const char *message, ...) { f_(stack_buf); return; } +#endif char *heap_buf = (char*)malloc((need+1) * sizeof(char)); if (heap_buf == NULL) { +#ifdef _MSC_VER + va_start(ap, message); + vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap); + va_end(ap); +#endif // Malloc failed. We might as well print the stack buffer. f_(stack_buf); return;