diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp index bc17e5758..6ea4e0dae 100644 --- a/src/zm_utils.cpp +++ b/src/zm_utils.cpp @@ -120,16 +120,18 @@ std::string Join(const StringVector &values, const std::string &delim) { std::string stringtf(const char* format, ...) { va_list args; va_start(args, format); - int size = vsnprintf(nullptr, 0, format, args) + 1; // Extra space for '\0' - va_end(args); - - if (size <= 0) { - throw std::runtime_error("Error during formatting."); - } - - std::unique_ptr buf(new char[size]); va_list args2; va_copy(args2, args); + int size = vsnprintf(nullptr, 0, format, args); + va_end(args); + + if (size < 0) { + va_end(args2); + throw std::runtime_error("Error during formatting."); + } + size += 1; // Extra space for '\0' + + std::unique_ptr buf(new char[size]); vsnprintf(buf.get(), size, format, args2); va_end(args2);