summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan G Rennison <j.g.rennison@gmail.com>2020-06-18 17:23:50 +0100
committerCharles Pigott <charlespigott@googlemail.com>2020-07-09 16:08:01 +0100
commit053d4f3bff97014bbf68b499302e93c6a9aea8b8 (patch)
tree2839a6ce1c0d18dc35373c61d8e5de90e0f4447d
parentde4dc792a9e8e1740a82cb8ef6e9884d26d23f87 (diff)
downloadopenttd-053d4f3bff97014bbf68b499302e93c6a9aea8b8.tar.xz
Fix: Thread unsafe use of SendPacket for PACKET_SERVER_MAP_SIZE
NetworkTCPSocketHandler::SendPacket is not thread safe and may not be used concurrently from multiple threads without suitable locking
-rw-r--r--src/network/network_server.cpp16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index 36a15d3ae..c82c51cfd 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -153,6 +153,16 @@ struct PacketWriter : SaveFilter {
this->current = nullptr;
}
+ /** Prepend the current packet to the queue. */
+ void PrependQueue()
+ {
+ if (this->current == nullptr) return;
+
+ this->current->next = this->packets;
+ this->packets = this->current;
+ this->current = nullptr;
+ }
+
void Write(byte *buf, size_t size) override
{
/* We want to abort the saving when the socket is closed. */
@@ -193,9 +203,9 @@ struct PacketWriter : SaveFilter {
this->AppendQueue();
/* Fast-track the size to the client. */
- Packet *p = new Packet(PACKET_SERVER_MAP_SIZE);
- p->Send_uint32((uint32)this->total_size);
- this->cs->NetworkTCPSocketHandler::SendPacket(p);
+ this->current = new Packet(PACKET_SERVER_MAP_SIZE);
+ this->current->Send_uint32((uint32)this->total_size);
+ this->PrependQueue();
}
};