Skip to content

Commit c84cbdb

Browse files
committed
Reducing buffer overflows
1 parent 3282cfc commit c84cbdb

21 files changed

+171
-178
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ V 1.51-beta5
3131
* added: Strafing combat to evade from being easy target bots
3232
* added: Linux bot slow-motion fix by w00tguy
3333
* changed: Increased the bot's flexibility
34-
* changed: Reduced redundant coding
34+
* changed: Reduced redundant coding and risks of buffer overflow crashes
3535
* changed: Reverted basemonster.h that was modified for Sven Coop
3636
* changed: The bot add or spawn delay for 8-10 seconds to prevent crashes
3737
* changed: Increasing bot's viewing angle to detect enemies better
@@ -44,7 +44,9 @@ V 1.51-beta5
4444
* TODO: To allow the Onos to be more aggressive and devour humans
4545
* TODO: To allow bots to use first aid mounted on the wall or `func_healthcharger`
4646
* TODO: To improve on how to allow bots climb ladders more properly
47+
* TODO: To prevent bots from team shooting in Team Deathmatch mode for HLDM, Op4DM, DMC and TS
4748
* TODO: To prevent bots in TS v3.0 on firing empty rounds and lying proning on floors
49+
* TODO: To bring back the TS v3.0 stunt dive code that was working previously
4850
* TODO: To allow bots in Op4 to use Grapple by using the "Fly" waypoint
4951
* TODO: To add proper support for S&I, FLF and WizWars
5052

dlls/bot.cpp

+24-24
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ void CBot::loadLearnedData() const
10611061
char tmp_filename[64];
10621062
char filename[256];
10631063

1064-
std::sprintf(tmp_filename, "%d.rld", m_Profile.m_iProfileId);
1064+
snprintf(tmp_filename, sizeof(tmp_filename), "%d.rld", m_Profile.m_iProfileId);
10651065

10661066
UTIL_BuildFileName(filename, BOT_PROFILES_FOLDER, tmp_filename);
10671067

@@ -1132,7 +1132,7 @@ void CBot::saveLearnedData() const
11321132
char tmp_filename[64];
11331133
char filename[256];
11341134

1135-
std::sprintf(tmp_filename, "%d.rld", m_Profile.m_iProfileId);
1135+
snprintf(tmp_filename, sizeof(tmp_filename), "%d.rld", m_Profile.m_iProfileId);
11361136

11371137
UTIL_BuildFileName(filename, BOT_PROFILES_FOLDER, tmp_filename);
11381138

@@ -3748,7 +3748,7 @@ void CBot::Think()
37483748
if (m_pEnemy == nullptr)
37493749
{
37503750
// Make sure the bot's not shooting an enemy before listening to sounds.
3751-
edict_t* pPlayer = nullptr;//TODO: Unused? [APG]RoboCop[CL]
3751+
edict_t* pPlayer;
37523752

37533753
if (GetClimbType() == BOT_CLIMB_NONE && (!PlayerStandingOnMe() && !StandingOnPlayer()) && m_fListenToSoundTime < gpGlobals->time && !m_iOrderType)
37543754
{
@@ -4978,8 +4978,8 @@ void CBot::LookForNewTasks()
49784978
if (UTIL_SpeciesOnTeam(AVH_USER3_ALIEN_PLAYER2) >= UTIL_PlayersOnTeam(TEAM_ALIEN) *
49794979
gBotGlobals.m_fGorgeAmount)
49804980
bGoGorge = false;
4981-
else if (this->m_Profile.m_GorgePercent > 0)
4982-
bGoGorge = true;
4981+
//else if (this->m_Profile.m_GorgePercent > 0)
4982+
// bGoGorge = true;
49834983

49844984
if (bGoGorge)
49854985
{
@@ -9781,7 +9781,7 @@ void BugMessage(edict_t* pEntity, const char* fmt, ...)
97819781
static char string[1024];
97829782

97839783
va_start(argptr, fmt);
9784-
std::vsprintf(string, fmt, argptr);
9784+
vsnprintf(string, sizeof(string), fmt, argptr);
97859785
va_end(argptr);
97869786

97879787
BotMessage(pEntity, 0, "%s%s%s%s", "BUG: ", string, " Report bugs to : ", BOT_AUTHOR);
@@ -9797,7 +9797,7 @@ void AssertMessage(const BOOL bAssert, char* fmt, ...)
97979797
static char string[1024];
97989798

97999799
va_start(argptr, fmt);
9800-
vsprintf(string, fmt, argptr);
9800+
vsnprintf(string, sizeof(string), fmt, argptr);
98019801
va_end(argptr);
98029802

98039803
BugMessage(nullptr, "Assertion Failed : %s", string);
@@ -9811,46 +9811,46 @@ void DebugMessage(int iDebugLevel, edict_t* pEntity, int errorlevel, const char*
98119811
char szDebugMsg[32];
98129812

98139813
va_start(argptr, fmt);
9814-
std::vsprintf(string, fmt, argptr);
9814+
vsnprintf(string, sizeof(string), fmt, argptr);
98159815
va_end(argptr);
98169816

98179817
switch (iDebugLevel)
98189818
{
98199819
case BOT_DEBUG_TOUCH_LEVEL:
98209820
// Bot touched object
9821-
std::sprintf(szDebugMsg, "%s:TOUCH]=>", BOT_DEBUG_TAG);
9821+
snprintf(szDebugMsg, sizeof(szDebugMsg), "%s:TOUCH]=>", BOT_DEBUG_TAG);
98229822
break;
98239823
case BOT_DEBUG_THINK_LEVEL:
98249824
// Bot thinks
9825-
std::sprintf(szDebugMsg, "%s:THINK]=>", BOT_DEBUG_TAG);
9825+
snprintf(szDebugMsg, sizeof(szDebugMsg), "%s:THINK]=>", BOT_DEBUG_TAG);
98269826
break;
98279827
case BOT_DEBUG_HEAR_LEVEL:
98289828
// Bot hears a sound
9829-
std::sprintf(szDebugMsg, "%s:HEAR]=>", BOT_DEBUG_TAG);
9829+
snprintf(szDebugMsg, sizeof(szDebugMsg), "%s:HEAR]=>", BOT_DEBUG_TAG);
98309830
break;
98319831
case BOT_DEBUG_MESSAGE_LEVEL:
98329832
// Bot recieves net message
9833-
std::sprintf(szDebugMsg, "%s:MESSAGE]=>", BOT_DEBUG_TAG);
9833+
snprintf(szDebugMsg, sizeof(szDebugMsg), "%s:MESSAGE]=>", BOT_DEBUG_TAG);
98349834
break;
98359835
case BOT_DEBUG_BLOCK_LEVEL:
98369836
// Bot blocks object
9837-
std::sprintf(szDebugMsg, "%s:BLOCK]=>", BOT_DEBUG_TAG);
9837+
snprintf(szDebugMsg, sizeof(szDebugMsg), "%s:BLOCK]=>", BOT_DEBUG_TAG);
98389838
break;
98399839
case BOT_DEBUG_MOVE_LEVEL:
98409840
// Bot moves somewhere
9841-
std::sprintf(szDebugMsg, "%s:MOVE]=>", BOT_DEBUG_TAG);
9841+
snprintf(szDebugMsg, sizeof(szDebugMsg), "%s:MOVE]=>", BOT_DEBUG_TAG);
98429842
break;
98439843
case BOT_DEBUG_AIM_LEVEL:
98449844
// Bot aims at something
9845-
std::sprintf(szDebugMsg, "%s:AIM]=>", BOT_DEBUG_TAG);
9845+
snprintf(szDebugMsg, sizeof(szDebugMsg), "%s:AIM]=>", BOT_DEBUG_TAG);
98469846
break;
98479847
case BOT_DEBUG_NAV_LEVEL:
98489848
// Bot touches/finds waypoints
9849-
std::sprintf(szDebugMsg, "%s:NAV]=>", BOT_DEBUG_TAG);
9849+
snprintf(szDebugMsg, sizeof(szDebugMsg), "%s:NAV]=>", BOT_DEBUG_TAG);
98509850
break;
98519851
case BOT_DEBUG_SEE_LEVEL:
98529852
// Bot touches/finds waypoints
9853-
std::sprintf(szDebugMsg, "%s:SEE]=>", BOT_DEBUG_TAG);
9853+
snprintf(szDebugMsg, sizeof(szDebugMsg), "%s:SEE]=>", BOT_DEBUG_TAG);
98549854
break;
98559855
}
98569856

@@ -9863,7 +9863,7 @@ void BotPrintTalkMessage(char* fmt, ...)
98639863
static char string[1024];
98649864

98659865
va_start(argptr, fmt);
9866-
vsprintf(string, fmt, argptr);
9866+
vsnprintf(string, sizeof(string), fmt, argptr);
98679867
va_end(argptr);
98689868

98699869
edict_t* pPlayer;
@@ -9888,7 +9888,7 @@ void BotPrintTalkMessageOne(edict_t* pClient, const char* fmt, ...)
98889888
static char string[1024];
98899889

98909890
va_start(argptr, fmt);
9891-
vsprintf(string, fmt, argptr);
9891+
vsnprintf(string, sizeof(string), fmt, argptr);
98929892
va_end(argptr);
98939893

98949894
if (pClient == nullptr)
@@ -9925,7 +9925,7 @@ void BotMessage(edict_t* pEntity, int errorlevel, const char* fmt, ...)
99259925
static char string[1024];
99269926

99279927
va_start(argptr, fmt);
9928-
std::vsprintf(string, fmt, argptr);
9928+
vsnprintf(string, sizeof(string), fmt, argptr);
99299929
va_end(argptr);
99309930

99319931
if (pEntity != nullptr)
@@ -10589,7 +10589,7 @@ CBotSquad* CBotSquads::AddSquadMember(edict_t* pLeader, edict_t* pMember)
1058910589
pClient->AddNewToolTip(BOT_TOOL_TIP_SQUAD_HELP);
1059010590
}
1059110591

10592-
std::sprintf(msg, "%s %s has joined your squad", BOT_DBG_MSG_TAG, STRING(pMember->v.netname));
10592+
snprintf(msg, sizeof(msg), "%s %s has joined your squad", BOT_DBG_MSG_TAG, STRING(pMember->v.netname));
1059310593
ClientPrint(pLeader, HUD_PRINTTALK, msg);
1059410594

1059510595
while (!tempStack.IsEmpty())
@@ -10777,7 +10777,7 @@ void CBot::BotOnLadder()
1077710777

1077810778
if (m_siLadderDir == LADDER_UP) // is the bot currently going up?
1077910779
{
10780-
pev->v_angle.x = -60.0f; // look upwards
10780+
pev->v_angle.x = -80.0f; // look upwards
1078110781

1078210782
//TODO: to replace 'moved_distance' and 'prev_speed' [APG]RoboCop[CL]
1078310783
// check if the bot hasn't moved much since the last location...
@@ -10791,7 +10791,7 @@ void CBot::BotOnLadder()
1079110791
}
1079210792
else if (m_siLadderDir == LADDER_DOWN) // is the bot currently going down?
1079310793
{
10794-
pev->v_angle.x = 60.0f; // look downwards
10794+
pev->v_angle.x = 80.0f; // look downwards
1079510795

1079610796
// check if the bot hasn't moved much since the last location...
1079710797
/*if (moved_distance <= 1 && prev_speed >= 1.0f)
@@ -10804,7 +10804,7 @@ void CBot::BotOnLadder()
1080410804
}
1080510805
else // the bot hasn't picked a direction yet, try going up...
1080610806
{
10807-
pev->v_angle.x = -60.0f; // look upwards
10807+
pev->v_angle.x = -80.0f; // look upwards
1080810808
m_siLadderDir = LADDER_UP;
1080910809
}
1081010810

dlls/bot.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6787,7 +6787,7 @@ class CBotGlobals
67876787

67886788
void buildFileName(const char* in_filename, char* out_filename)
67896789
{
6790-
std::sprintf(out_filename, "%s%s", m_szBotFolder, in_filename);
6790+
snprintf(out_filename, sizeof(out_filename), "%s%s", m_szBotFolder, in_filename);
67916791
}
67926792

67936793
const char* botFolder() const

dlls/bot_client.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@
100100

101101
// types of damage to ignore...
102102
#define IGNORE_DAMAGE (DMG_CRUSH | DMG_FREEZE | DMG_FALL | \
103-
DMG_DROWN | DMG_NERVEGAS | DMG_RADIATION | \
104-
DMG_DROWNRECOVER | DMG_ACID | DMG_SLOWBURN | \
105-
DMG_SLOWFREEZE | 0xFF000000)
103+
DMG_DROWN | DMG_NERVEGAS | DMG_RADIATION | \
104+
DMG_DROWNRECOVER | DMG_ACID | DMG_SLOWBURN | \
105+
DMG_SLOWFREEZE | 0xFF000000)
106106

107107
//extern bot_weapon_t weapon_defs[MAX_WEAPONS]; // array of weapon definitions
108108

dlls/bot_commands.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ eBotCvarState BotFunc_AddBot(CClient* pClient, const char* arg1, const char* arg
17861786
{
17871787
iBotProfile = CanUseIds.Random();
17881788

1789-
std::sprintf(szBotProfile, "%d.ini", iBotProfile);
1789+
snprintf(szBotProfile, sizeof(szBotProfile), "%d.ini", iBotProfile);
17901790

17911791
UTIL_BuildFileName(szBotProfilePath, "botprofiles", szBotProfile);
17921792

@@ -1819,7 +1819,7 @@ eBotCvarState BotFunc_AddBot(CClient* pClient, const char* arg1, const char* arg
18191819
iTeam = pBot->m_Profile.m_iFavTeam;
18201820
iClass = pBot->m_Profile.m_iClass;
18211821

1822-
std::sprintf(szBotProfile, "%d.ini", iBotProfile);
1822+
snprintf(szBotProfile, sizeof(szBotProfile), "%d.ini", iBotProfile);
18231823

18241824
UTIL_BuildFileName(szProfileToOpen, "botprofiles", szBotProfile);
18251825

@@ -1970,9 +1970,9 @@ eBotCvarState BotFunc_AddBot(CClient* pClient, const char* arg1, const char* arg
19701970

19711971
char szColour[5];
19721972

1973-
std::sprintf(szColour, "%d", pBot->m_Profile.m_iTopColour);
1973+
snprintf(szColour, sizeof(szColour), "%d", pBot->m_Profile.m_iTopColour);
19741974
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "topcolor", szColour);
1975-
std::sprintf(szColour, "%d", pBot->m_Profile.m_iBottomColour);
1975+
snprintf(szColour, sizeof(szColour), "%d", pBot->m_Profile.m_iBottomColour);
19761976
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "bottomcolor", szColour);
19771977

19781978
#ifdef RCBOT_META_BUILD

dlls/bot_const.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1418,8 +1418,8 @@ enum
14181418
};
14191419

14201420
#define BEGIN_SEARCH_THROUGH_PLAYERS(Variable) \
1421-
int i; \
1422-
for ( i = 1; i <= gpGlobals->maxClients; i ++ ) \
1421+
int i; \
1422+
for ( i = 1; i <= gpGlobals->maxClients; i ++ ) \
14231423
{ \
14241424
(Variable) = INDEXENT(i);\
14251425
if ( (Variable) == NULL )\

dlls/bot_menu.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void CBotMenu::Render(CClient* pClient)
396396

397397
pClient->m_pMenu = this;
398398

399-
std::sprintf(szMenuText, "%s\n-----\nOptions:\n", m_szCaption);
399+
snprintf(szMenuText, sizeof(szMenuText), "%s\n-----\nOptions:\n", m_szCaption);
400400

401401
for (int i = 0; i < 10; i++)
402402
{
@@ -408,9 +408,9 @@ void CBotMenu::Render(CClient* pClient)
408408
iSlots |= 1 << (i - 1);
409409

410410
if (m_Menus[i]->HasNextMenu())
411-
std::sprintf(szMenuItemText, "%d. %s...\n", i, m_Menus[i]->GetCaption());
411+
snprintf(szMenuText, sizeof(szMenuItemText), "%d. %s...\n", i, m_Menus[i]->GetCaption());
412412
else
413-
std::sprintf(szMenuItemText, "%d. %s\n", i, m_Menus[i]->GetCaption());
413+
snprintf(szMenuText, sizeof(szMenuItemText), "%d. %s\n", i, m_Menus[i]->GetCaption());
414414

415415
std::strcat(szMenuText, szMenuItemText);
416416
}
@@ -989,7 +989,7 @@ void BotMenu_Func_Squad_RemoveAllBotSquads(CClient* pClient)
989989
}
990990
}
991991

992-
std::sprintf(msg, "%s %d bot squads removed\n", BOT_DBG_MSG_TAG, iCount);
992+
snprintf(msg, sizeof(msg), "%s %d bot squads removed\n", BOT_DBG_MSG_TAG, iCount);
993993
ClientPrint(pClient->GetPlayer(), HUD_PRINTTALK, msg);
994994
}
995995

dlls/bot_navigate.cpp

+12-19
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ BOOL BotNavigate_UpdateWaypoint(CBot* pBot)
11241124

11251125
Vector vBotOrigin = pBot->pev->origin;
11261126
Vector vWptOrigin;
1127-
Vector vMoveVector;
1127+
//Vector vMoveVector; //Unused? [APG]RoboCop[CL]
11281128

11291129
int iCurrWpt;
11301130
int iWptFlags;
@@ -1184,17 +1184,13 @@ BOOL BotNavigate_UpdateWaypoint(CBot* pBot)
11841184
{
11851185
//Clear this waypoint, get a new one and flush path info.
11861186

1187-
PATH* pFailed = BotNavigate_FindPathFromTo(pBot->m_iPrevWaypointIndex, pBot->m_iCurrentWaypointIndex, pBot->m_iTeam);//TODO: triggers crash? [APG]RoboCopCL]
1188-
1189-
if (pFailed)
1187+
if (PATH* pFailed = BotNavigate_FindPathFromTo(pBot->m_iPrevWaypointIndex, pBot->m_iCurrentWaypointIndex, pBot->m_iTeam))
11901188
pBot->m_stFailedPaths.AddFailedPath(pFailed);
11911189

11921190
pBot->m_iCurrentWaypointIndex = WaypointLocations.NearestWaypoint(vBotOrigin, REACHABLE_RANGE, pBot->m_iLastFailedWaypoint, true, false, true);
11931191
iCurrWpt = pBot->m_iCurrentWaypointIndex;
11941192

1195-
CBotTask* pCurrentTask = pBot->m_Tasks.CurrentTask();
1196-
1197-
if (pCurrentTask)
1193+
if (CBotTask* pCurrentTask = pBot->m_Tasks.CurrentTask())
11981194
{
11991195
pCurrentTask->SetPathInfo(false);
12001196
}
@@ -1236,7 +1232,7 @@ BOOL BotNavigate_UpdateWaypoint(CBot* pBot)
12361232

12371233
fDistance = 0;
12381234

1239-
vMoveVector = vWptOrigin;
1235+
//Vector vMoveVector = vWptOrigin; //Unused? [APG]RoboCop[CL]
12401236

12411237
// bot is not climbing
12421238
if (pBot->GetClimbType() != BOT_CLIMB_NONE)
@@ -1510,9 +1506,8 @@ BOOL BotNavigate_UpdateWaypoint(CBot* pBot)
15101506
if (!pBot->m_Tasks.HasTask(BOT_TASK_PUSH_PUSHABLE))
15111507
{
15121508
// get nearest pushable
1513-
edict_t* pPushable = UTIL_FindNearestEntity(szEntity, 1, vWptOrigin, 512, false);
15141509

1515-
if (pPushable)
1510+
if (edict_t* pPushable = UTIL_FindNearestEntity(szEntity, 1, vWptOrigin, 512, false))
15161511
{
15171512
// if its too far away from the waypoint push it to the waypoint
15181513
if (!UTIL_AcceptablePushableVector(pPushable, vWptOrigin))
@@ -1531,9 +1526,7 @@ BOOL BotNavigate_UpdateWaypoint(CBot* pBot)
15311526
// Reached objective waypoint???
15321527
if (pBot->m_iWaypointGoalIndex == pBot->m_iCurrentWaypointIndex)
15331528
{
1534-
CBotTask* m_CurrentTask = pBot->m_Tasks.CurrentTask();
1535-
1536-
if (m_CurrentTask)
1529+
if (CBotTask* m_CurrentTask = pBot->m_Tasks.CurrentTask())
15371530
{
15381531
if (m_CurrentTask->Task() == BOT_TASK_FIND_PATH)
15391532
{
@@ -1837,7 +1830,7 @@ Vector BotNavigate_ScanFOV(CBot* pBot)
18371830

18381831
vAngles = pBot->m_pEdict->v.angles;
18391832

1840-
vAngles.y = vAngles.y + (fAngle - iMaxStep / 2);
1833+
vAngles.y = vAngles.y + (fAngle - static_cast<float>(iMaxStep) / 2);
18411834

18421835
UTIL_FixFloatAngle(&vAngles.y);
18431836

@@ -1879,7 +1872,7 @@ Vector BotNavigate_ScanFOV(CBot* pBot)
18791872

18801873
vAngles = UTIL_VecToAngles(pBot->m_pEdict->v.velocity);
18811874

1882-
vAngles.y = vAngles.y + (fAngle - iMaxStep / 2);
1875+
vAngles.y = vAngles.y + (fAngle - static_cast<float>(iMaxStep) / 2);
18831876

18841877
UTIL_FixFloatAngle(&vAngles.y);
18851878

@@ -2083,9 +2076,9 @@ BOOL CheckLift(CBot* pBot, Vector vCheckOrigin, const Vector& vCheckToOrigin)
20832076
char* szClassnames[3] = { "func_button","button_target","func_rot_button" };
20842077

20852078
// check nearby the lift button waypoint
2086-
edict_t* pButton = UTIL_FindNearestEntity(szClassnames, 3, WaypointOrigin(iWpt), fRange, true);
2079+
edict_t* button = UTIL_FindNearestEntity(szClassnames, 3, WaypointOrigin(iWpt), fRange, true);
20872080

2088-
if (pButton)
2081+
if (button)
20892082
{
20902083
// if a button if found use this one
20912084

@@ -2094,8 +2087,8 @@ BOOL CheckLift(CBot* pBot, Vector vCheckOrigin, const Vector& vCheckToOrigin)
20942087
// Bot was below the lift so wait for the lift to descend
20952088
pBot->AddPriorityTask(CBotTask(BOT_TASK_WAIT_FOR_ENTITY, iScheduleId, pHit));
20962089
pBot->AddPriorityTask(CBotTask(BOT_TASK_FIND_PATH, iScheduleId, nullptr, iWpt, -1));
2097-
pBot->AddPriorityTask(CBotTask(BOT_TASK_USE, iScheduleId, pButton));
2098-
pBot->AddPriorityTask(CBotTask(BOT_TASK_FIND_PATH, iScheduleId, pButton));
2090+
pBot->AddPriorityTask(CBotTask(BOT_TASK_USE, iScheduleId, button));
2091+
pBot->AddPriorityTask(CBotTask(BOT_TASK_FIND_PATH, iScheduleId, button));
20992092

21002093
// make sure we update these tasks so we know we are using a lift
21012094
pBot->m_Tasks.GiveSchedIdDescription(iScheduleId, BOT_SCHED_USE_LIFT);

0 commit comments

Comments
 (0)