From f174d71ac739a775b91d4a324af5bd1aeddc8039 Mon Sep 17 00:00:00 2001 From: Jonathan Li Date: Tue, 21 Mar 2017 20:29:09 +0000 Subject: [PATCH] gsdx: Fix vsnprintf usage in format function -1 is only returned when there is an encoding error, and the va_list argument is indeterminate after being passed to vsnprintf. Use the return value to determine the buffer length, and call va_end and then va_start before vsnprintf is called again. --- plugins/GSdx/stdafx.cpp | 31 +++++++++---------------------- plugins/GSdx/stdafx.h | 2 +- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/plugins/GSdx/stdafx.cpp b/plugins/GSdx/stdafx.cpp index 777b2140bb..6126d73435 100644 --- a/plugins/GSdx/stdafx.cpp +++ b/plugins/GSdx/stdafx.cpp @@ -28,35 +28,22 @@ // TODO: reference any additional headers you need in STDAFX.H // and not in this file -string format(const char* fmt, ...) +std::string format(const char* fmt, ...) { va_list args; + va_start(args, fmt); - - int result = -1, length = 256; - - char* buffer = NULL; - - while(result == -1) - { - if(buffer) delete [] buffer; - - buffer = new char[length + 1]; - - memset(buffer, 0, length + 1); - - result = vsnprintf(buffer, length, fmt, args); - - length *= 2; - } - + int size = vsnprintf(nullptr, 0, fmt, args) + 1; va_end(args); - string s(buffer); + assert(size > 0); + std::vector buffer(std::max(1, size)); - delete [] buffer; + va_start(args, fmt); + vsnprintf(buffer.data(), size, fmt, args); + va_end(args); - return s; + return {buffer.data()}; } #ifdef _WIN32 diff --git a/plugins/GSdx/stdafx.h b/plugins/GSdx/stdafx.h index 0dec92cb67..f4c42debb0 100644 --- a/plugins/GSdx/stdafx.h +++ b/plugins/GSdx/stdafx.h @@ -365,7 +365,7 @@ using namespace std; #endif -extern string format(const char* fmt, ...); +extern std::string format(const char* fmt, ...); extern void* vmalloc(size_t size, bool code); extern void vmfree(void* ptr, size_t size);