Skip to content

Commit 7b2fd72

Browse files
committed
- Cleanup and safety tweaks for NetMesh and settings file IO
1 parent aa7357f commit 7b2fd72

4 files changed

Lines changed: 46 additions & 20 deletions

File tree

Core/Libraries/Source/WWVegas/WWLib/mutex.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,21 @@ CriticalSectionClass::CriticalSectionClass() : handle(nullptr), locked(false)
9595
#ifdef _UNIX
9696
//assert(0);
9797
#else
98-
handle=W3DNEWARRAY char[sizeof(CRITICAL_SECTION)];
99-
InitializeCriticalSection((CRITICAL_SECTION*)handle);
98+
// Allocate with proper alignment for CRITICAL_SECTION
99+
// On 64-bit: requires 8-byte alignment, on 32-bit: requires 4-byte alignment
100+
#if defined(_WIN64) || defined(__LP64__)
101+
size_t alignment = 8;
102+
#else
103+
size_t alignment = 4;
104+
#endif
105+
106+
handle = _aligned_malloc(sizeof(CRITICAL_SECTION), alignment);
107+
if (handle != nullptr) {
108+
InitializeCriticalSection((CRITICAL_SECTION*)handle);
109+
}
110+
else {
111+
WWASSERT(false); // Allocation failed
112+
}
100113
#endif
101114
}
102115

@@ -105,9 +118,11 @@ CriticalSectionClass::~CriticalSectionClass()
105118
#ifdef _UNIX
106119
//assert(0);
107120
#else
108-
WWASSERT(!locked); // Can't delete locked mutex!
109-
DeleteCriticalSection((CRITICAL_SECTION*)handle);
110-
delete[] handle;
121+
WWASSERT(!locked); // Can't delete locked critical section!
122+
if (handle != nullptr) {
123+
DeleteCriticalSection((CRITICAL_SECTION*)handle);
124+
_aligned_free(handle);
125+
}
111126
#endif
112127
}
113128

GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NGMP_Helpers.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ std::vector<uint8_t> Base64Decode(const std::string& encodedData) {
150150
uint8_t charArray4[4], charArray3[3];
151151

152152
while (inLen-- && (encodedData[in_] != '=') && isBase64(encodedData[in_])) {
153+
if (i >= 4) break;
153154
charArray4[i++] = encodedData[in_]; in_++;
154155
if (i == 4) {
155156
for (i = 0; i < 4; i++)

GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NetworkMesh.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -753,9 +753,10 @@ QueuedGamePacket NetworkMesh::RecvGamePacket()
753753

754754
int NetworkMesh::SendGamePacket(void* pBuffer, uint32_t totalDataSize, int64_t user_id)
755755
{
756-
if (m_mapConnections.contains(user_id))
756+
auto it = m_mapConnections.find(user_id);
757+
if (it != m_mapConnections.end())
757758
{
758-
return m_mapConnections[user_id].SendGamePacket(pBuffer, totalDataSize);
759+
return it->second.SendGamePacket(pBuffer, totalDataSize);
759760
}
760761

761762
return -2;
@@ -765,21 +766,22 @@ int NetworkMesh::SendGamePacket(void* pBuffer, uint32_t totalDataSize, int64_t u
765766
void NetworkMesh::StartConnectionSignalling(int64_t remoteUserID, uint16_t preferredPort)
766767
{
767768
// if we already have a connection to this use, drop it, having a single-direction connection will break signalling
768-
if (m_mapConnections.find(remoteUserID) != m_mapConnections.end())
769+
auto it = m_mapConnections.find(remoteUserID);
770+
if (it != m_mapConnections.end())
769771
{
770-
if (m_mapConnections[remoteUserID].m_hSteamConnection != k_HSteamNetConnection_Invalid)
772+
if (it->second.m_hSteamConnection != k_HSteamNetConnection_Invalid)
771773
{
772774
NetworkLog(ELogVerbosity::LOG_RELEASE, "[DC] Closing connection %lld, new connection is being negotiated", remoteUserID);
773-
SteamNetworkingSockets()->CloseConnection(m_mapConnections[remoteUserID].m_hSteamConnection, 0, "Client Disconnecting Gracefully (new connection being negotiated)", false);
775+
SteamNetworkingSockets()->CloseConnection(it->second.m_hSteamConnection, 0, "Client Disconnecting Gracefully (new connection being negotiated)", false);
774776

775777
if (TheNetwork != nullptr)
776778
{
777779
TheNetwork->GetConnectionManager()->disconnectPlayer(remoteUserID);
778780
}
779781
}
780782

781-
NetworkLog(ELogVerbosity::LOG_RELEASE, "[ERASE 3] Removing user %lld", m_mapConnections[remoteUserID].m_userID);
782-
m_mapConnections.erase(remoteUserID);
783+
NetworkLog(ELogVerbosity::LOG_RELEASE, "[ERASE 3] Removing user %lld", it->second.m_userID);
784+
m_mapConnections.erase(it);
783785
}
784786

785787
NGMP_OnlineServicesManager* pOnlineServicesMgr = NGMP_OnlineServicesManager::GetInstance();
@@ -887,14 +889,18 @@ void NetworkMesh::DisconnectUser(int64_t remoteUserID)
887889

888890
if (TheNGMPGame && !TheNGMPGame->isGameInProgress())
889891
{
890-
for (auto& kvPair : m_mapConnections)
892+
for (auto it = m_mapConnections.begin(); it != m_mapConnections.end(); )
891893
{
892-
if (kvPair.second.m_userID == remoteUserID)
894+
if (it->second.m_userID == remoteUserID)
893895
{
894-
NetworkLog(ELogVerbosity::LOG_RELEASE, "[ERASE] Removing user %lld", kvPair.second.m_userID);
895-
m_mapConnections.erase(kvPair.first);
896+
NetworkLog(ELogVerbosity::LOG_RELEASE, "[ERASE] Removing user %lld", it->second.m_userID);
897+
it = m_mapConnections.erase(it);
896898
break;
897899
}
900+
else
901+
{
902+
++it;
903+
}
898904
}
899905
}
900906
}

GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/OnlineServices_Init.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ void NGMP_OnlineServicesManager::CaptureScreenshotToDisk()
120120
{
121121
// write to disk
122122
FILE* pFile = fopen(strFilePath.c_str(), "wb");
123-
fwrite(vecBuffer.data(), sizeof(uint8_t), vecBuffer.size(), pFile);
124-
fclose(pFile);
123+
if (pFile != nullptr) {
124+
fwrite(vecBuffer.data(), sizeof(uint8_t), vecBuffer.size(), pFile);
125+
fclose(pFile);
126+
}
125127
}
126128
});
127129
}
@@ -426,8 +428,10 @@ void NGMP_OnlineServicesManager::ContinueUpdate()
426428
}
427429

428430
FILE* pFile = fopen(strOutPath.c_str(), "wb");
429-
fwrite(vecBuffer.data(), sizeof(uint8_t), bufSize, pFile);
430-
fclose(pFile);
431+
if (pFile != nullptr) {
432+
fwrite(vecBuffer.data(), sizeof(uint8_t), bufSize, pFile);
433+
fclose(pFile);
434+
}
431435

432436
// call continue update again, thisll check if we're done or have more work to do
433437
ContinueUpdate();

0 commit comments

Comments
 (0)