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

@ -160,15 +160,15 @@ BufferedImage::BufferedImage(const wstring& File, bool filenameHasExtension /*=f
name = wDrive + L"res" + filePath.substr(0,filePath.length()-4) + mipMapPath + L".png";
}
const char *pchTextureName=wstringtofilename(name);
auto pchTextureName=wstringtofilename(name);
#ifndef _CONTENT_PACKAGE
app.DebugPrintf("\n--- Loading TEXTURE - %s\n\n",pchTextureName);
app.DebugPrintf("\n--- Loading TEXTURE - %s\n\n",pchTextureName.c_str());
#endif
D3DXIMAGE_INFO ImageInfo;
ZeroMemory(&ImageInfo,sizeof(D3DXIMAGE_INFO));
hr=RenderManager.LoadTextureData(pchTextureName,&ImageInfo,&data[l]);
hr=RenderManager.LoadTextureData(pchTextureName.c_str(), &ImageInfo, &data[l]);
if(hr!=ERROR_SUCCESS)

View file

@ -588,9 +588,9 @@ int LevelGenerationOptions::packMounted(LPVOID pParam,int iPad,DWORD dwErr,DWORD
nullptr // Unsupported
);
#else
const char *pchFilename=wstringtofilename(grf.getPath());
std::string pchFilename=wstringtofilename(grf.getPath());
HANDLE fileHandle = CreateFile(
pchFilename, // file name
pchFilename.c_str(), // file name
GENERIC_READ, // access mode
0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but...
nullptr, // Unused
@ -640,9 +640,9 @@ int LevelGenerationOptions::packMounted(LPVOID pParam,int iPad,DWORD dwErr,DWORD
nullptr // Unsupported
);
#else
const char *pchFilename=wstringtofilename(save.getPath());
auto pchFilename=wstringtofilename(save.getPath());
HANDLE fileHandle = CreateFile(
pchFilename, // file name
pchFilename.c_str(), // file name
GENERIC_READ, // access mode
0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but...
nullptr, // Unused

View file

@ -259,9 +259,9 @@ bool CGameNetworkManager::StartNetworkGame(Minecraft *minecraft, LPVOID lpParame
nullptr // Unsupported
);
#else
const char *pchFilename=wstringtofilename(grf.getPath());
std::string pchFilename=wstringtofilename(grf.getPath());
HANDLE fileHandle = CreateFile(
pchFilename, // file name
pchFilename.c_str(), // file name
GENERIC_READ, // access mode
0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but...
nullptr, // Unused

View file

@ -368,9 +368,9 @@ int DLCTexturePack::packMounted(LPVOID pParam,int iPad,DWORD dwErr,DWORD dwLicen
nullptr // Unsupported
);
#else
const char *pchFilename=wstringtofilename(grf.getPath());
std::string pchFilename=wstringtofilename(grf.getPath());
HANDLE fileHandle = CreateFile(
pchFilename, // file name
pchFilename.c_str(), // file name
GENERIC_READ, // access mode
0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but...
nullptr, // Unused
@ -420,9 +420,9 @@ int DLCTexturePack::packMounted(LPVOID pParam,int iPad,DWORD dwErr,DWORD dwLicen
nullptr // Unsupported
);
#else
const char *pchFilename=wstringtofilename(grf.getPath());
auto pchFilename=wstringtofilename(grf.getPath());
HANDLE fileHandle = CreateFile(
pchFilename, // file name
pchFilename.c_str(), // file name
GENERIC_READ, // access mode
0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but...
nullptr, // Unused

View file

@ -81,8 +81,8 @@ void Settings::saveProperties()
stream << "# MinecraftConsoles dedicated server properties\r\n";
for (unordered_map<wstring, wstring>::const_iterator it = properties.begin(); it != properties.end(); ++it)
{
string key = string(wstringtochararray(it->first));
string value = string(wstringtochararray(it->second));
string key = wstringtofilename(it->first);
string value = wstringtofilename(it->second);
stream << key << "=" << value << "\r\n";
}
}

View file

@ -1400,7 +1400,7 @@ BufferedImage *Textures::readImage(TEXTURE_NAME texId, const wstring& name) // 4
}
else
{
const char *pchName=wstringtofilename(name);
std::string pchName=wstringtofilename(name);
#ifdef __PS3__
if(app.GetBootedFromDiscPatch() && app.IsFileInPatchList(pchName))
{
@ -1423,7 +1423,7 @@ BufferedImage *Textures::readImage(TEXTURE_NAME texId, const wstring& name) // 4
drive = skins->getDefault()->getPath(isTu);
}
const char *pchDrive=wstringtofilename(drive);
std::string pchDrive=wstringtofilename(drive);
if(IsOriginalImage(texId, name) || isTu)
{

View file

@ -1390,14 +1390,14 @@ static int RunHeadlessServer()
Settings serverSettings(new File(L"server.properties"));
const wstring configuredBindIp = serverSettings.getString(L"server-ip", L"");
const char* bindIp = "*";
std::string bindIp = "*";
if (g_Win64DedicatedServerBindIP[0] != 0)
{
bindIp = g_Win64DedicatedServerBindIP;
}
else if (!configuredBindIp.empty())
{
bindIp = wstringtochararray(configuredBindIp);
bindIp = wstringtostring(configuredBindIp);
}
const int port = g_Win64DedicatedServerPort > 0 ? g_Win64DedicatedServerPort : serverSettings.getInt(L"server-port", WIN64_NET_DEFAULT_PORT);

View file

@ -105,11 +105,11 @@ Connection::Connection(Socket *socket, const wstring& id, PacketListener *packet
m_hWakeReadThread = new C4JThread::Event;
m_hWakeWriteThread = new C4JThread::Event;
const char *szId = wstringtofilename(id);
std::string szId = wstringtofilename(id);
char readThreadName[256];
char writeThreadName[256];
sprintf_s(readThreadName, sizeof(readThreadName), "%.240s read\n", szId);
sprintf_s(writeThreadName, sizeof(writeThreadName), "%.240s write\n", szId);
sprintf_s(readThreadName, sizeof(readThreadName), "%.240s read\n", szId.c_str());
sprintf_s(writeThreadName, sizeof(writeThreadName), "%.240s write\n", szId.c_str());
readThread = new C4JThread(runRead, static_cast<void *>(this), readThreadName, READ_STACK_SIZE);
writeThread = new C4JThread(runWrite, this, writeThreadName, WRITE_STACK_SIZE);

View file

@ -881,10 +881,10 @@ void ConsoleSaveFileOriginal::DebugFlushToFile(void *compressedData /*= nullptr*
wstring wtemp = targetFileDir.getPath() + wstring(fileName);
LPCWSTR lpFileName = wtemp.c_str();
#else
LPCSTR lpFileName = wstringtofilename( targetFileDir.getPath() + wstring(fileName) );
std::string lpFileName = wstringtofilename( targetFileDir.getPath() + wstring(fileName) );
#endif
#ifndef __PSVITA__
HANDLE hSaveFile = CreateFile( lpFileName, GENERIC_WRITE, 0, nullptr, OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr);
HANDLE hSaveFile = CreateFile( lpFileName.c_str(), GENERIC_WRITE, 0, nullptr, OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr);
#endif
if(compressedData != nullptr && compressedDataSize > 0)

View file

@ -37,7 +37,7 @@ File::File( const wstring& pathname ) //: parent( nullptr )
m_abstractPathName = pathname;
#ifdef _WINDOWS64
string path = wstringtochararray(m_abstractPathName);
string path = wstringtofilename(m_abstractPathName);
string finalPath = StorageManager.GetMountedPath(path.c_str());
if(finalPath.size() == 0) finalPath = path;
m_abstractPathName = convStringToWstring(finalPath);
@ -100,7 +100,7 @@ bool File::_delete()
#ifdef _UNICODE
BOOL result = DeleteFile( getPath().c_str() );
#else
BOOL result = DeleteFile( wstringtofilename(getPath()) );
BOOL result = DeleteFile( wstringtofilename(getPath()).c_str() );
#endif
if( result == 0 )
{
@ -123,7 +123,7 @@ bool File::mkdir() const
return CreateDirectory( getPath().c_str(), nullptr) != 0;
#else
return CreateDirectory( wstringtofilename(getPath()), nullptr) != 0;
return CreateDirectory( wstringtofilename(getPath()).c_str(), nullptr) != 0;
#endif
}
@ -175,9 +175,10 @@ bool File::mkdirs() const
}
}
#else
if( GetFileAttributes( wstringtofilename(pathToHere) ) == -1 )
auto fileName = wstringtofilename(pathToHere);
if( GetFileAttributes( fileName.c_str() ) == -1 )
{
DWORD result = CreateDirectory( wstringtofilename(pathToHere), nullptr);
DWORD result = CreateDirectory( fileName.c_str(), nullptr);
if( result == 0 )
{
// Failed to create
@ -210,7 +211,7 @@ bool File::exists() const
return GetFileAttributes( getPath().c_str() ) != -1;
#else
return GetFileAttributes( wstringtofilename(getPath()) ) != -1;
return GetFileAttributes( wstringtofilename(getPath()).c_str() ) != -1;
#endif
}
@ -237,12 +238,12 @@ bool File::renameTo(File dest)
// called, therefore we were getting sourcePath and destPath having the same value. The solution here is to
// make a copy of the sourcePath by storing it in a std::string
std::string sourcePath = wstringtofilename(getPath());
const char *destPath = wstringtofilename(dest.getPath());
std::string destPath = wstringtofilename(dest.getPath());
#ifdef _DURANGO
__debugbreak(); // TODO
BOOL result = false;
#else
BOOL result = MoveFile(sourcePath.c_str(), destPath);
BOOL result = MoveFile(sourcePath.c_str(), destPath.c_str());
#endif
if( result == 0 )
@ -386,7 +387,7 @@ std::vector<File *> *File::listFiles() const
}
#else
char path[MAX_PATH] {};
snprintf( path, MAX_PATH, "%s\\*", wstringtofilename( getPath() ) );
snprintf( path, MAX_PATH, "%s\\*", wstringtofilename( getPath() ).c_str() );
HANDLE hFind = FindFirstFile( path, &wfd);
if(hFind != INVALID_HANDLE_VALUE)
{
@ -513,7 +514,7 @@ bool File::isDirectory() const
#ifdef _UNICODE
return exists() && ( GetFileAttributes( getPath().c_str() ) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
#else
return exists() && ( GetFileAttributes( wstringtofilename(getPath()) ) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
return exists() && ( GetFileAttributes( wstringtofilename(getPath()).c_str() ) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
#endif
}
@ -596,7 +597,7 @@ int64_t File::length()
);
#else
BOOL result = GetFileAttributesEx(
wstringtofilename(getPath()), // file or directory name
wstringtofilename(getPath()).c_str(), // file or directory name
GetFileExInfoStandard, // attribute
&fileInfoBuffer // attribute information
);
@ -634,7 +635,7 @@ int64_t File::lastModified()
);
#else
BOOL result = GetFileAttributesEx(
wstringtofilename(getPath()), // file or directory name
wstringtofilename(getPath()).c_str(), // file or directory name
GetFileExInfoStandard, // attribute
&fileInfoBuffer // attribute information
);

View file

@ -18,7 +18,7 @@
//SecurityException - if a security manager exists and its checkRead method denies read access to the file.
FileInputStream::FileInputStream(const File &file)
{
const char *pchFilename=wstringtofilename(file.getPath());
std::string pchFilename=wstringtofilename(file.getPath());
#ifdef _UNICODE
m_fileHandle = CreateFile(
file.getPath().c_str(), // file name
@ -31,7 +31,7 @@ FileInputStream::FileInputStream(const File &file)
);
#else
m_fileHandle = CreateFile(
pchFilename, // file name
pchFilename.c_str(), // file name
GENERIC_READ, // access mode
0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but...
nullptr, // Unused

View file

@ -31,7 +31,7 @@ FileOutputStream::FileOutputStream(const File &file) : m_fileHandle( INVALID_HAN
);
#else
m_fileHandle = CreateFile(
wstringtofilename(file.getPath()) , // file name
wstringtofilename(file.getPath()).c_str(), // file name
GENERIC_WRITE, // access mode
0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but...
nullptr, // Unused

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;
}

View file

@ -23,8 +23,9 @@ template <class T> T _fromHEXString(const std::wstring& s)
}
wstring convStringToWstring(const string& converting);
const char *wstringtofilename(const wstring& name);
const char *wstringtochararray(const wstring& name);
const std::string wstringtofilename(const wstring& name);
const std::string wstringtostring(const wstring& name);
const vector<char> wstringtochararray(const wstring& name);
wstring filenametowstring(const char *name);
std::vector<std::wstring> &stringSplit(const std::wstring &s, wchar_t delim, std::vector<std::wstring> &elems);

View file

@ -60,8 +60,8 @@ std::vector<BYTE> ZipFile::extract(const std::wstring* name) {
return buffer;
}
catch (exception e) {
OutputDebugString(wstringtochararray(L"Error extracting file from zip: " + *name + L'\n'));
OutputDebugString(wstringtochararray(L"Exception: " + convStringToWstring(e.what()) + L'\n'));
app.DebugPrintf(wstringtostring(L"Error extracting file from zip: " + *name + L'\n').c_str());
app.DebugPrintf(wstringtostring(L"Exception: " + convStringToWstring(e.what()) + L'\n').c_str());
__debugbreak();
return {};
}
@ -132,8 +132,8 @@ bool ZipFile::open(File* file) {
}
}
catch (exception e) {
OutputDebugString(wstringtochararray(L"Error opening zip file: " + file->getPath()));
OutputDebugString(wstringtochararray(L"\nException: " + convStringToWstring(e.what())));
app.DebugPrintf(wstringtostring(L"Error opening zip file: " + file->getPath()).c_str());
app.DebugPrintf(wstringtostring(L"\nException: " + convStringToWstring(e.what())).c_str());
__debugbreak();
}
return false;