Network: Defer deletion of client and server game socket handler

This fixes various use after free scenarios in error handling paths
This commit is contained in:
Jonathan G Rennison
2023-06-16 20:44:48 +01:00
parent 532d3881cd
commit 495db43b72
5 changed files with 40 additions and 7 deletions

View File

@@ -21,6 +21,8 @@
#include "../../safeguards.h"
static std::vector<NetworkGameSocketHandler *> _deferred_deletions;
static const char* _packet_game_type_names[] {
"SERVER_FULL",
"SERVER_BANNED",
@@ -290,3 +292,17 @@ void NetworkGameSocketHandler::LogSentPacket(const Packet &pkt)
PacketGameType type = (PacketGameType)pkt.GetPacketType();
DEBUG(net, 5, "[tcp/game] sent packet type %d (%s) to client %d, %s", type, GetPacketGameTypeName(type), this->client_id, this->GetDebugInfo().c_str());
}
void NetworkGameSocketHandler::DeferDeletion()
{
_deferred_deletions.push_back(this);
this->is_pending_deletion = true;
}
/* static */ void NetworkGameSocketHandler::ProcessDeferredDeletions()
{
for (NetworkGameSocketHandler *cs : _deferred_deletions) {
delete cs;
}
_deferred_deletions.clear();
}

View File

@@ -168,7 +168,8 @@ public:
class NetworkGameSocketHandler : public NetworkTCPSocketHandler {
/* TODO: rewrite into a proper class */
private:
NetworkClientInfo *info; ///< Client info related to this socket
NetworkClientInfo *info; ///< Client info related to this socket
bool is_pending_deletion = false; ///< Whether this socket is pending deletion
protected:
bool ignore_close = false;
@@ -592,6 +593,11 @@ public:
virtual std::string GetDebugInfo() const;
virtual void LogSentPacket(const Packet &pkt) override;
bool IsPendingDeletion() const { return this->is_pending_deletion; }
void DeferDeletion();
static void ProcessDeferredDeletions();
};
#endif /* NETWORK_CORE_TCP_GAME_H */