From c07c942659ccf758d118bfcdff5df076c3e4c36c Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 21 May 2022 14:38:25 +1000 Subject: [PATCH] Common/StringUtil: Handle error returns in StdStringFromFormatV() --- common/StringUtil.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/common/StringUtil.cpp b/common/StringUtil.cpp index 7e2c6c2187..8290f9ecc9 100644 --- a/common/StringUtil.cpp +++ b/common/StringUtil.cpp @@ -49,8 +49,14 @@ namespace StringUtil va_end(ap_copy); std::string ret; - ret.resize(len); - std::vsnprintf(ret.data(), ret.size() + 1, format, ap); + + // If an encoding error occurs, len is -1. Which we definitely don't want to resize to. + if (len > 0) + { + ret.resize(len); + std::vsnprintf(ret.data(), ret.size() + 1, format, ap); + } + return ret; }