mirror of https://github.com/PCSX2/pcsx2.git
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.
This commit is contained in:
parent
ae2e8469b4
commit
f174d71ac7
|
@ -28,35 +28,22 @@
|
||||||
// TODO: reference any additional headers you need in STDAFX.H
|
// TODO: reference any additional headers you need in STDAFX.H
|
||||||
// and not in this file
|
// and not in this file
|
||||||
|
|
||||||
string format(const char* fmt, ...)
|
std::string format(const char* fmt, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
|
int size = vsnprintf(nullptr, 0, fmt, args) + 1;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
string s(buffer);
|
assert(size > 0);
|
||||||
|
std::vector<char> 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
|
#ifdef _WIN32
|
||||||
|
|
|
@ -365,7 +365,7 @@ using namespace std;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern string format(const char* fmt, ...);
|
extern std::string format(const char* fmt, ...);
|
||||||
|
|
||||||
extern void* vmalloc(size_t size, bool code);
|
extern void* vmalloc(size_t size, bool code);
|
||||||
extern void vmfree(void* ptr, size_t size);
|
extern void vmfree(void* ptr, size_t size);
|
||||||
|
|
Loading…
Reference in New Issue