From d65216df190b0ff1522098c8a552594ce29feb3d Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Tue, 4 Jun 2013 22:25:06 +0200 Subject: [PATCH] THRIFT-1982 vsnprintf on Windows have different semantics Patch: Konrad Grochowski --- lib/cpp/src/thrift/Thrift.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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; -- 2.17.1