From 0e54190541c59367eae3b3082a9b1feb9efab24e Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 16 Jul 2023 23:49:18 +0200 Subject: [PATCH] Add clamps to 'CC_CreateFakePlayer_f' Make sure user doesn't create bots past MAX_PLAYERS, also clamp team number as otherwise the game would crash as well. --- r5dev/common/callback.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/r5dev/common/callback.cpp b/r5dev/common/callback.cpp index da4b8780..91bffade 100644 --- a/r5dev/common/callback.cpp +++ b/r5dev/common/callback.cpp @@ -1406,9 +1406,26 @@ void CC_CreateFakePlayer_f(const CCommand& args) return; } + int numPlayers = g_pServer->GetNumClients(); + + // Already at max, don't create. + if (numPlayers >= g_ServerGlobalVariables->m_nMaxClients) + return; + + const char* playerName = args.Arg(1); + + int teamNum = atoi(args.Arg(2)); + int maxTeams = int(g_pServer->GetMaxTeams()) + 1; + + // Clamp team count, going above the limit will + // cause a crash. Going below 0 means that the + // engine will assign the bot to the last team. + if (teamNum > maxTeams) + teamNum = maxTeams; + g_pEngineServer->LockNetworkStringTables(true); - edict_t nHandle = g_pEngineServer->CreateFakeClient(args.Arg(1), atoi(args.Arg(2))); + edict_t nHandle = g_pEngineServer->CreateFakeClient(playerName, teamNum); g_pServerGameClients->ClientFullyConnect(nHandle, false); g_pEngineServer->LockNetworkStringTables(false);