THRIFT-1982 vsnprintf on Windows have different semantics
authorRoger Meier <roger@apache.org>
Tue, 4 Jun 2013 20:25:06 +0000 (22:25 +0200)
committerRoger Meier <roger@apache.org>
Tue, 4 Jun 2013 20:25:06 +0000 (22:25 +0200)
Patch: Konrad Grochowski

lib/cpp/src/thrift/Thrift.cpp

index bcbdb1a..6c7f8ae 100644 (file)
@@ -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;