From b2fd4f1f5ce63886e0cb07fa59d7f22b8ad57e52 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 3 Jan 2018 13:11:22 +0100 Subject: [PATCH] Fix a bug with lol::vformat() adding two terminating null chars. --- src/base/string.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/base/string.cpp b/src/base/string.cpp index fe51c7d3..706ade5a 100644 --- a/src/base/string.cpp +++ b/src/base/string.cpp @@ -103,9 +103,9 @@ std::string vformat(char const *format, va_list ap) ap2 = ap; #endif - /* vsnprintf() tells us how many character we need, and we need to - * add one for the terminating null byte. */ - size_t needed = vsnprintf(nullptr, 0, format, ap2) + 1; + /* vsnprintf() tells us how many characters we need, not counting + * the terminating null character. */ + size_t needed = vsnprintf(nullptr, 0, format, ap2); #if defined va_copy || !defined _MSC_VER /* do not call va_end() if va_copy() wasn't called. */ @@ -114,7 +114,7 @@ std::string vformat(char const *format, va_list ap) std::string ret; ret.resize(needed); - vsnprintf(&ret[0], needed, format, ap); + vsnprintf(&ret[0], needed + 1, format, ap); return ret; }