From da69771af628c6d16b5840f3d642ef70fcbe2170 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 23 May 2013 13:19:12 -0400 Subject: Some systems with whacky va_list implementations do undefined stuff when vsnprintf is called multiple times with the same list, so have InspIRCd::Format va_copy the list before each call --- src/helperfuncs.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index cc760c535..e86c0bc70 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -401,10 +401,22 @@ unsigned long InspIRCd::Duration(const std::string &str) const char* InspIRCd::Format(va_list &vaList, const char* formatString) { static std::vector formatBuffer(1024); - int vsnret = 0; - while ((vsnret = vsnprintf(&formatBuffer[0], formatBuffer.size(), formatString, vaList)) < 0 || static_cast(vsnret) >= formatBuffer.size()) + + while (true) + { + va_list dst; + va_copy(dst, vaList); + + int vsnret = vsnprintf(&formatBuffer[0], formatBuffer.size(), formatString, dst); + if (vsnret > 0 && static_cast(vsnret) < formatBuffer.size()) + { + return &formatBuffer[0]; + } + formatBuffer.resize(formatBuffer.size() * 2); - return &formatBuffer[0]; + } + + throw CoreException(); } const char* InspIRCd::Format(const char* formatString, ...) -- cgit v1.2.3