Skip to content

Commit

Permalink
get rid of unsafe string functions
Browse files Browse the repository at this point in the history
minor refactor
  • Loading branch information
s1lentq committed Sep 12, 2024
1 parent 3cf66de commit 19714af
Show file tree
Hide file tree
Showing 40 changed files with 268 additions and 365 deletions.
2 changes: 1 addition & 1 deletion regamedll/dlls/bot/cs_bot_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ void CCSBot::SpawnBot()
TheCSBots()->ValidateMapData();
ResetValues();

Q_strcpy(m_name, STRING(pev->netname));
Q_strlcpy(m_name, STRING(pev->netname));

SetState(&m_buyState);
SetTouch(&CCSBot::BotTouch);
Expand Down
8 changes: 4 additions & 4 deletions regamedll/dlls/bot/cs_bot_learn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,14 @@ void CCSBot::StartSaveProcess()

void CCSBot::UpdateSaveProcess()
{
char filename[256];
char msg[256];
char cmd[128];

GET_GAME_DIR(filename);
char gd[64]{};
GET_GAME_DIR(gd);

Q_strcat(filename, "\\");
Q_strcat(filename, TheBots->GetNavMapFilename());
char filename[MAX_OSPATH];
Q_snprintf(filename, sizeof(filename), "%s\\%s", gd, TheBots->GetNavMapFilename());

HintMessageToAllPlayers("Saving...");
SaveNavigationMap(filename);
Expand Down
14 changes: 8 additions & 6 deletions regamedll/dlls/bot/cs_bot_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,16 @@ void CCSBotManager::ServerCommand(const char *pcmd)
}
else if (FStrEq(pcmd, "bot_nav_save"))
{
GET_GAME_DIR(buffer);
Q_strcat(buffer, "\\");
Q_strcat(buffer, CBotManager::GetNavMapFilename());
char gd[64]{};
GET_GAME_DIR(gd);

if (SaveNavigationMap(buffer))
CONSOLE_ECHO("Navigation map '%s' saved.\n", buffer);
char filename[MAX_OSPATH];
Q_snprintf(filename, sizeof(filename), "%s\\%s", gd, CBotManager::GetNavMapFilename());

if (SaveNavigationMap(filename))
CONSOLE_ECHO("Navigation map '%s' saved.\n", filename);
else
CONSOLE_ECHO("ERROR: Cannot save navigation map '%s'.\n", buffer);
CONSOLE_ECHO("ERROR: Cannot save navigation map '%s'.\n", filename);
}
else if (FStrEq(pcmd, "bot_nav_load"))
{
Expand Down
27 changes: 12 additions & 15 deletions regamedll/dlls/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ NOXREF int CountTeams()

void ListPlayers(CBasePlayer *current)
{
char message[120] = "", cNumber[12];
char message[120]{};

CBaseEntity *pEntity = nullptr;
while ((pEntity = UTIL_FindEntityByClassname(pEntity, "player")))
Expand All @@ -479,12 +479,7 @@ void ListPlayers(CBasePlayer *current)
CBasePlayer *pPlayer = GetClassPtr<CCSPlayer>((CBasePlayer *)pEntity->pev);
int iUserID = GETPLAYERUSERID(ENT(pPlayer->pev));

Q_sprintf(cNumber, "%d", iUserID);
Q_strcpy(message, "\n");
Q_strcat(message, cNumber);
Q_strcat(message, " : ");
Q_strcat(message, STRING(pPlayer->pev->netname));

Q_snprintf(message, sizeof(message), "\n%d : %s", iUserID, STRING(pPlayer->pev->netname));
ClientPrint(current->pev, HUD_PRINTCONSOLE, message);
}

Expand Down Expand Up @@ -738,8 +733,8 @@ void EXT_FUNC ClientPutInServer(edict_t *pEntity)

pPlayer->m_iJoiningState = SHOWLTEXT;

static char sName[128];
Q_strcpy(sName, STRING(pPlayer->pev->netname));
char sName[128];
Q_strlcpy(sName, STRING(pPlayer->pev->netname));

for (char *pApersand = sName; pApersand && *pApersand != '\0'; pApersand++)
{
Expand Down Expand Up @@ -797,12 +792,12 @@ void Host_Say(edict_t *pEntity, BOOL teamonly)
{
if (CMD_ARGC_() >= 2)
{
Q_sprintf(szTemp, "%s %s", pcmd, CMD_ARGS());
Q_snprintf(szTemp, sizeof(szTemp), "%s %s", pcmd, CMD_ARGS());
}
else
{
// Just a one word command, use the first word...sigh
Q_sprintf(szTemp, "%s", pcmd);
Q_snprintf(szTemp, sizeof(szTemp), "%s", pcmd);
}

p = szTemp;
Expand Down Expand Up @@ -967,8 +962,8 @@ void Host_Say(edict_t *pEntity, BOOL teamonly)
}
}

Q_strcat(text, p);
Q_strcat(text, "\n");
Q_strlcat(text, p);
Q_strlcat(text, "\n");

// loop through all players
// Start with the first player.
Expand Down Expand Up @@ -4993,7 +4988,7 @@ void EXT_FUNC UpdateClientData(const edict_t *ent, int sendweapons, struct clien
cd->flSwimTime = pev->flSwimTime;
cd->waterjumptime = int(pev->teleport_time);

Q_strcpy(cd->physinfo, ENGINE_GETPHYSINFO(ent));
Q_strlcpy(cd->physinfo, ENGINE_GETPHYSINFO(ent));

cd->maxspeed = pev->maxspeed;
cd->fov = pev->fov;
Expand Down Expand Up @@ -5204,8 +5199,10 @@ int EXT_FUNC InconsistentFile(const edict_t *pEdict, const char *filename, char
if (!CVAR_GET_FLOAT("mp_consistency"))
return 0;

const int BufferLen = 256;

// Default behavior is to kick the player
Q_sprintf(disconnect_message, "Server is enforcing file consistency for %s\n", filename);
Q_snprintf(disconnect_message, BufferLen, "Server is enforcing file consistency for %s\n", filename);

// Kick now with specified disconnect message.
return 1;
Expand Down
14 changes: 7 additions & 7 deletions regamedll/dlls/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ NOXREF void UTIL_DPrintf(DebugOutputType outputType, char *pszMsg, ...)
{
va_list argptr;
va_start(argptr, pszMsg);
vsprintf(theDebugBuffer, pszMsg, argptr);
Q_vsnprintf(theDebugBuffer, sizeof(theDebugBuffer), pszMsg, argptr);
va_end(argptr);

SERVER_PRINT(theDebugBuffer);
Expand All @@ -41,7 +41,7 @@ void UTIL_DPrintf(char *pszMsg, ...)

va_list argptr;
va_start(argptr, pszMsg);
vsprintf(theDebugBuffer, pszMsg, argptr);
Q_vsnprintf(theDebugBuffer, sizeof(theDebugBuffer), pszMsg, argptr);
va_end(argptr);

SERVER_PRINT(theDebugBuffer);
Expand Down Expand Up @@ -130,7 +130,7 @@ NOXREF void UTIL_BotDPrintf(char *pszMsg, ...)
{
va_list argptr;
va_start(argptr, pszMsg);
vsprintf(theDebugBuffer, pszMsg, argptr);
Q_vsnprintf(theDebugBuffer, sizeof(theDebugBuffer), pszMsg, argptr);
va_end(argptr);

SERVER_PRINT(theDebugBuffer);
Expand All @@ -146,7 +146,7 @@ void UTIL_CareerDPrintf(char *pszMsg, ...)
{
va_list argptr;
va_start(argptr, pszMsg);
vsprintf(theDebugBuffer, pszMsg, argptr);
Q_vsnprintf(theDebugBuffer, sizeof(theDebugBuffer), pszMsg, argptr);
va_end(argptr);

SERVER_PRINT(theDebugBuffer);
Expand All @@ -162,7 +162,7 @@ NOXREF void UTIL_TutorDPrintf(char *pszMsg, ...)
{
va_list argptr;
va_start(argptr, pszMsg);
vsprintf(theDebugBuffer, pszMsg, argptr);
Q_vsnprintf(theDebugBuffer, sizeof(theDebugBuffer), pszMsg, argptr);
va_end(argptr);

SERVER_PRINT(theDebugBuffer);
Expand All @@ -178,7 +178,7 @@ NOXREF void UTIL_StatsDPrintf(char *pszMsg, ...)
{
va_list argptr;
va_start(argptr, pszMsg);
vsprintf(theDebugBuffer, pszMsg, argptr);
Q_vsnprintf(theDebugBuffer, sizeof(theDebugBuffer), pszMsg, argptr);
va_end(argptr);

SERVER_PRINT(theDebugBuffer);
Expand All @@ -194,7 +194,7 @@ NOXREF void UTIL_HostageDPrintf(char *pszMsg, ...)
{
va_list argptr;
va_start(argptr, pszMsg);
vsprintf(theDebugBuffer, pszMsg, argptr);
Q_vsnprintf(theDebugBuffer, sizeof(theDebugBuffer), pszMsg, argptr);
va_end(argptr);

SERVER_PRINT(theDebugBuffer);
Expand Down
2 changes: 1 addition & 1 deletion regamedll/dlls/explode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void ExplosionCreate(const Vector &center, Vector &angles, edict_t *pOwner, int

CBaseEntity *pExplosion = CBaseEntity::Create("env_explosion", center, angles, pOwner);

Q_sprintf(buf, "%3d", magnitude);
Q_snprintf(buf, sizeof(buf), "%3d", magnitude);

kvd.szKeyName = "iMagnitude";
kvd.szValue = buf;
Expand Down
4 changes: 2 additions & 2 deletions regamedll/dlls/gamerules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ CGameRules::CGameRules()
m_bBombDropped = FALSE;
m_bGameOver = false;

m_GameDesc = new char[sizeof("Counter-Strike")];
Q_strcpy(m_GameDesc, AreRunningCZero() ? "Condition Zero" : "Counter-Strike");
const char *pszGameDesc = AreRunningCZero() ? "Condition Zero" : "Counter-Strike";
m_GameDesc = CloneString(pszGameDesc);
}

CGameRules::~CGameRules()
Expand Down
1 change: 0 additions & 1 deletion regamedll/dlls/gamerules.h
Original file line number Diff line number Diff line change
Expand Up @@ -1009,5 +1009,4 @@ char *GetTeam(int team);
void DestroyMapCycle(mapcycle_t *cycle);
int ReloadMapCycleFile(char *filename, mapcycle_t *cycle);
int CountPlayers();
void ExtractCommandString(char *s, char *szCommand);
int GetMapCount();
6 changes: 3 additions & 3 deletions regamedll/dlls/hostage/hostage_improv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1605,9 +1605,9 @@ void CHostageImprov::Afraid()

int which = RANDOM_LONG(0, 100) % 3 + 1;

Q_sprintf(animInto, "cower_into_%d", which);
Q_sprintf(animLoop, "cower_loop_%d", which);
Q_sprintf(animExit, "cower_exit_%d", which);
Q_snprintf(animInto, sizeof(animInto), "cower_into_%d", which);
Q_snprintf(animLoop, sizeof(animLoop), "cower_loop_%d", which);
Q_snprintf(animExit, sizeof(animExit), "cower_exit_%d", which);

m_animateState.AddSequence(this, animInto);
m_animateState.AddSequence(this, animLoop, RANDOM_FLOAT(3, 10));
Expand Down
2 changes: 1 addition & 1 deletion regamedll/dlls/items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ BOOL CItemBattery::MyTouch(CBasePlayer *pPlayer)
pct--;

char szcharge[64];
Q_sprintf(szcharge, "!HEV_%1dP", pct);
Q_snprintf(szcharge, sizeof(szcharge), "!HEV_%1dP", pct);
pPlayer->SetSuitUpdate(szcharge, SUIT_SENTENCE, SUIT_NEXT_IN_30SEC);

return TRUE;
Expand Down
12 changes: 6 additions & 6 deletions regamedll/dlls/lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ void CEnvLight::KeyValue(KeyValueData *pkvd)
pkvd->fHandled = TRUE;

char szColor[64];
Q_sprintf(szColor, "%d", r);
Q_snprintf(szColor, sizeof(szColor), "%d", r);
CVAR_SET_STRING("sv_skycolor_r", szColor);
Q_sprintf(szColor, "%d", g);
Q_snprintf(szColor, sizeof(szColor), "%d", g);
CVAR_SET_STRING("sv_skycolor_g", szColor);
Q_sprintf(szColor, "%d", b);
Q_snprintf(szColor, sizeof(szColor), "%d", b);
CVAR_SET_STRING("sv_skycolor_b", szColor);
}
else
Expand All @@ -147,13 +147,13 @@ void CEnvLight::Spawn()
char szVector[64];
UTIL_MakeAimVectors(pev->angles);

Q_sprintf(szVector, "%f", gpGlobals->v_forward.x);
Q_snprintf(szVector, sizeof(szVector), "%f", gpGlobals->v_forward.x);
CVAR_SET_STRING("sv_skyvec_x", szVector);

Q_sprintf(szVector, "%f", gpGlobals->v_forward.y);
Q_snprintf(szVector, sizeof(szVector), "%f", gpGlobals->v_forward.y);
CVAR_SET_STRING("sv_skyvec_y", szVector);

Q_sprintf(szVector, "%f", gpGlobals->v_forward.z);
Q_snprintf(szVector, sizeof(szVector), "%f", gpGlobals->v_forward.z);
CVAR_SET_STRING("sv_skyvec_z", szVector);

CLight::Spawn();
Expand Down
Loading

0 comments on commit 19714af

Please sign in to comment.