Skip to content

Commit b8818e3

Browse files
Fix crash in SV_SetClient_Ping by pre-linking client pointers
SV_CalcPings in the engine can run before ClientConnect has fully linked the entity to the client structure, causing a null pointer dereference when accessing client->edict->client->ping. This change ensures that g_entities[i].client pointers are set immediately upon client array allocation in AllocateClientArray, and cleared in FreeClientArray. This guarantees that valid client slots always have a valid client pointer for the engine to access.
1 parent f020431 commit b8818e3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/server/gameplay/g_clients.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,24 @@ void AllocateClientArray(int maxClients) {
6464
game.maxLagOrigins = ComputeLagHistorySamples();
6565
const std::size_t lagCount = static_cast<std::size_t>(game.maxClients) * static_cast<std::size_t>(game.maxLagOrigins);
6666
game.lagOrigins = static_cast<Vector3*>(TagMallocChecked(sizeof(Vector3) * lagCount));
67+
68+
// [KEX]: Ensure client pointers are linked immediately to prevent engine crashes
69+
// if SV_CalcPings runs before a client is fully connected.
70+
if (g_entities) {
71+
for (int i = 0; i < game.maxClients; i++) {
72+
g_entities[i + 1].client = &game.clients[i];
73+
}
74+
}
6775
}
6876

6977
void FreeClientArray() {
78+
// [KEX]: Unlink client pointers
79+
if (g_entities && game.clients) {
80+
for (int i = 0; i < game.maxClients; i++) {
81+
g_entities[i + 1].client = nullptr;
82+
}
83+
}
84+
7085
if (game.clients)
7186
DestroyClients(game.clients, game.maxClients);
7287

0 commit comments

Comments
 (0)