Remove static buffers from StringHelpers to prevent overwriting strings from another thread.

This commit is contained in:
Soggy_Pancake 2026-03-19 18:09:34 -07:00
parent 3f7745b262
commit 5d459c0ff9
15 changed files with 68 additions and 55 deletions

View file

@ -46,13 +46,11 @@ wstring convStringToWstring(const string& converting)
return converted;
}
// Convert for filename wstrings to a straight character pointer for Xbox APIs. The returned string is only valid until
// this function is called again, and it isn't thread-safe etc. as I'm just storing the returned name in a local static
// to save having to clear it up everywhere this is used.
const char *wstringtofilename(const wstring& name)
const std::string wstringtofilename(const wstring& name)
{
static char buf[256];
assert(name.length()<256);
std::string buf;
buf.reserve(name.length());
for(unsigned int i = 0; i < name.length(); i++ )
{
wchar_t c = name[i];
@ -62,23 +60,36 @@ const char *wstringtofilename(const wstring& name)
if(c=='/') c='\\';
#endif
assert(c<128); // Will we have to do any conversion of non-ASCII characters in filenames?
buf[i] = static_cast<char>(c);
buf += static_cast<char>(c);
}
buf[name.length()] = 0;
return buf;
}
const char *wstringtochararray(const wstring& name)
const std::string wstringtostring(const wstring& name)
{
assert(name.length() < 256);
std::string buf;
buf.reserve(name.length());
for (unsigned int i = 0; i < name.length(); i++)
{
wchar_t c = name[i];
assert(c < 128); // Will we have to do any conversion of non-ASCII characters in filenames?
buf += static_cast<char>(c);
}
return buf;
}
const vector<char> wstringtochararray(const wstring& name)
{
static char buf[256];
assert(name.length()<256);
vector<char> buf;
buf.reserve(name.length());
for(unsigned int i = 0; i < name.length(); i++ )
{
wchar_t c = name[i];
assert(c<128); // Will we have to do any conversion of non-ASCII characters in filenames?
buf[i] = static_cast<char>(c);
}
buf[name.length()] = 0;
return buf;
}