From 19714af6e65cd0bf857dfecd6a95ed343ca14687 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Fri, 13 Sep 2024 06:43:27 +0700 Subject: [PATCH] get rid of unsafe string functions minor refactor --- regamedll/dlls/bot/cs_bot_init.cpp | 2 +- regamedll/dlls/bot/cs_bot_learn.cpp | 8 +- regamedll/dlls/bot/cs_bot_manager.cpp | 14 +- regamedll/dlls/client.cpp | 27 ++-- regamedll/dlls/debug.cpp | 14 +- regamedll/dlls/explode.cpp | 2 +- regamedll/dlls/gamerules.cpp | 4 +- regamedll/dlls/gamerules.h | 1 - regamedll/dlls/hostage/hostage_improv.cpp | 6 +- regamedll/dlls/items.cpp | 2 +- regamedll/dlls/lights.cpp | 12 +- regamedll/dlls/multiplay_gamerules.cpp | 62 +++----- regamedll/dlls/observer.cpp | 16 +- regamedll/dlls/player.cpp | 141 ++++++------------ regamedll/dlls/player.h | 6 +- regamedll/dlls/saverestore.cpp | 6 +- regamedll/dlls/skill.cpp | 2 +- regamedll/dlls/sound.cpp | 43 ++---- regamedll/dlls/sound.h | 4 +- regamedll/dlls/triggers.cpp | 18 +-- regamedll/dlls/tutor_base_tutor.cpp | 10 +- regamedll/dlls/tutor_cs_tutor.cpp | 12 +- regamedll/dlls/util.cpp | 38 ++--- regamedll/dlls/weapons.cpp | 16 +- regamedll/dlls/world.cpp | 2 +- regamedll/dlls/wpn_shared/wpn_flashbang.cpp | 10 +- regamedll/dlls/wpn_shared/wpn_hegrenade.cpp | 10 +- regamedll/dlls/wpn_shared/wpn_knife.cpp | 48 +++--- regamedll/dlls/wpn_shared/wpn_m4a1.cpp | 4 +- .../dlls/wpn_shared/wpn_smokegrenade.cpp | 10 +- regamedll/dlls/wpn_shared/wpn_usp.cpp | 4 +- regamedll/game_shared/bot/bot.cpp | 8 +- regamedll/game_shared/bot/bot_manager.cpp | 2 +- regamedll/game_shared/bot/bot_profile.cpp | 7 +- regamedll/game_shared/bot/bot_util.cpp | 4 +- regamedll/game_shared/bot/nav_area.cpp | 4 +- regamedll/game_shared/bot/nav_file.cpp | 11 +- regamedll/pm_shared/pm_shared.cpp | 5 +- regamedll/public/strtools.h | 34 +++-- regamedll/unittests/mathfun_tests.cpp | 4 +- 40 files changed, 268 insertions(+), 365 deletions(-) diff --git a/regamedll/dlls/bot/cs_bot_init.cpp b/regamedll/dlls/bot/cs_bot_init.cpp index a6503e90..1bf7aec0 100644 --- a/regamedll/dlls/bot/cs_bot_init.cpp +++ b/regamedll/dlls/bot/cs_bot_init.cpp @@ -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); diff --git a/regamedll/dlls/bot/cs_bot_learn.cpp b/regamedll/dlls/bot/cs_bot_learn.cpp index 76edded7..3cf041e3 100644 --- a/regamedll/dlls/bot/cs_bot_learn.cpp +++ b/regamedll/dlls/bot/cs_bot_learn.cpp @@ -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); diff --git a/regamedll/dlls/bot/cs_bot_manager.cpp b/regamedll/dlls/bot/cs_bot_manager.cpp index b4391be7..5656667b 100644 --- a/regamedll/dlls/bot/cs_bot_manager.cpp +++ b/regamedll/dlls/bot/cs_bot_manager.cpp @@ -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")) { diff --git a/regamedll/dlls/client.cpp b/regamedll/dlls/client.cpp index 3fe161c6..bc223ed7 100644 --- a/regamedll/dlls/client.cpp +++ b/regamedll/dlls/client.cpp @@ -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"))) @@ -479,12 +479,7 @@ void ListPlayers(CBasePlayer *current) CBasePlayer *pPlayer = GetClassPtr((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); } @@ -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++) { @@ -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; @@ -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. @@ -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; @@ -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; diff --git a/regamedll/dlls/debug.cpp b/regamedll/dlls/debug.cpp index 82d50d71..81605143 100644 --- a/regamedll/dlls/debug.cpp +++ b/regamedll/dlls/debug.cpp @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/regamedll/dlls/explode.cpp b/regamedll/dlls/explode.cpp index 104d8472..6d1c7ccd 100644 --- a/regamedll/dlls/explode.cpp +++ b/regamedll/dlls/explode.cpp @@ -201,7 +201,7 @@ void ExplosionCreate(const Vector ¢er, 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; diff --git a/regamedll/dlls/gamerules.cpp b/regamedll/dlls/gamerules.cpp index 45210e21..3d9f1418 100644 --- a/regamedll/dlls/gamerules.cpp +++ b/regamedll/dlls/gamerules.cpp @@ -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() diff --git a/regamedll/dlls/gamerules.h b/regamedll/dlls/gamerules.h index a4f556ab..220e0955 100644 --- a/regamedll/dlls/gamerules.h +++ b/regamedll/dlls/gamerules.h @@ -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(); diff --git a/regamedll/dlls/hostage/hostage_improv.cpp b/regamedll/dlls/hostage/hostage_improv.cpp index 9cdba777..6932d218 100644 --- a/regamedll/dlls/hostage/hostage_improv.cpp +++ b/regamedll/dlls/hostage/hostage_improv.cpp @@ -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)); diff --git a/regamedll/dlls/items.cpp b/regamedll/dlls/items.cpp index 03620e36..ba138e04 100644 --- a/regamedll/dlls/items.cpp +++ b/regamedll/dlls/items.cpp @@ -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; diff --git a/regamedll/dlls/lights.cpp b/regamedll/dlls/lights.cpp index d44cddcc..aa8629ea 100644 --- a/regamedll/dlls/lights.cpp +++ b/regamedll/dlls/lights.cpp @@ -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 @@ -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(); diff --git a/regamedll/dlls/multiplay_gamerules.cpp b/regamedll/dlls/multiplay_gamerules.cpp index 8ba46556..7a84afd4 100644 --- a/regamedll/dlls/multiplay_gamerules.cpp +++ b/regamedll/dlls/multiplay_gamerules.cpp @@ -178,15 +178,12 @@ bool CCStrikeGameMgrHelper::GetCanHearPlayer(CBasePlayer* pListener, CBasePlayer void Broadcast(const char *sentence) { - char text[32]; + char text[128]; if (!sentence) - { return; - } - Q_strcpy(text, "%!MRAD_"); - Q_strcat(text, UTIL_VarArgs("%s", sentence)); + Q_snprintf(text, sizeof(text), "%%!MRAD_%s", sentence); MESSAGE_BEGIN(MSG_BROADCAST, gmsgSendAudio); WRITE_BYTE(0); @@ -507,7 +504,7 @@ CHalfLifeMultiplay::CHalfLifeMultiplay() char szCommand[256]; ALERT(at_console, "Executing listen server config file\n"); - Q_sprintf(szCommand, "exec %s\n", lservercfgfile); + Q_snprintf(szCommand, sizeof(szCommand), "exec %s\n", lservercfgfile); SERVER_COMMAND(szCommand); } } @@ -4554,12 +4551,7 @@ int ReloadMapCycleFile(char *filename, mapcycle_t *cycle) if (Q_strlen(pToken) <= 0) break; -#ifdef REGAMEDLL_FIXES - Q_strncpy(szMap, pToken, sizeof(szMap) - 1); - szMap[sizeof(szMap) - 1] = '\0'; -#else - Q_strcpy(szMap, pToken); -#endif + Q_strlcpy(szMap, pToken); // Any more tokens on this line? if (SharedTokenWaiting(pFileList)) @@ -4568,7 +4560,7 @@ int ReloadMapCycleFile(char *filename, mapcycle_t *cycle) if (Q_strlen(pToken) > 0) { hasBuffer = true; - Q_strcpy(szBuffer, pToken); + Q_strlcpy(szBuffer, pToken); } } @@ -4580,7 +4572,7 @@ int ReloadMapCycleFile(char *filename, mapcycle_t *cycle) item = new mapcycle_item_s; - Q_strcpy(item->mapname, szMap); + Q_strlcpy(item->mapname, szMap); item->minplayers = 0; item->maxplayers = 0; @@ -4610,7 +4602,7 @@ int ReloadMapCycleFile(char *filename, mapcycle_t *cycle) REMOVE_KEY_VALUE(szBuffer, "minplayers"); REMOVE_KEY_VALUE(szBuffer, "maxplayers"); - Q_strcpy(item->rulebuffer, szBuffer); + Q_strlcpy(item->rulebuffer, szBuffer); } item->next = cycle->items; @@ -4675,7 +4667,7 @@ int CountPlayers() } // Parse commands/key value pairs to issue right after map xxx command is issued on server level transition -void ExtractCommandString(char *s, char *szCommand) +void ExtractCommandString(char *s, char *szCommand, size_t len) { // Now make rules happen char pkey[512]; @@ -4744,13 +4736,13 @@ void ExtractCommandString(char *s, char *szCommand) *c = '\0'; - Q_strcat(szCommand, pkey); + Q_strlcat(szCommand, pkey, len); if (Q_strlen(value) > 0) { - Q_strcat(szCommand, " "); - Q_strcat(szCommand, value); + Q_strlcat(szCommand, " ", len); + Q_strlcat(szCommand, value, len); } - Q_strcat(szCommand, "\n"); + Q_strlcat(szCommand, "\n", len); /*if (!*s) { @@ -4937,10 +4929,10 @@ void EXT_FUNC CHalfLifeMultiplay::__API_HOOK(ChangeLevel)() #ifdef REGAMEDLL_FIXES // the absolute default level is de_dust - Q_strcpy(szFirstMapInList, "de_dust"); + Q_strlcpy(szFirstMapInList, "de_dust"); #else // the absolute default level is hldm1 - Q_strcpy(szFirstMapInList, "hldm1"); + Q_strlcpy(szFirstMapInList, "hldm1"); #endif int curplayers; @@ -4958,7 +4950,7 @@ void EXT_FUNC CHalfLifeMultiplay::__API_HOOK(ChangeLevel)() // Has the map cycle filename changed? if (Q_stricmp(mapcfile, szPreviousMapCycleFile) != 0) { - Q_strcpy(szPreviousMapCycleFile, mapcfile); + Q_strlcpy(szPreviousMapCycleFile, mapcfile); DestroyMapCycle(&mapcycle); @@ -4976,8 +4968,8 @@ void EXT_FUNC CHalfLifeMultiplay::__API_HOOK(ChangeLevel)() mapcycle_item_s *item; // Assume current map - Q_strcpy(szNextMap, STRING(gpGlobals->mapname)); - Q_strcpy(szFirstMapInList, STRING(gpGlobals->mapname)); + Q_strlcpy(szNextMap, STRING(gpGlobals->mapname)); + Q_strlcpy(szFirstMapInList, STRING(gpGlobals->mapname)); // Traverse list for (item = mapcycle.next_item; item->next != mapcycle.next_item; item = item->next) @@ -5030,14 +5022,14 @@ void EXT_FUNC CHalfLifeMultiplay::__API_HOOK(ChangeLevel)() mapcycle.next_item = item->next; // Perform logic on current item - Q_strcpy(szNextMap, item->mapname); - ExtractCommandString(item->rulebuffer, szCommands); - Q_strcpy(szRules, item->rulebuffer); + Q_strlcpy(szNextMap, item->mapname); + ExtractCommandString(item->rulebuffer, szCommands, sizeof(szCommands)); + Q_strlcpy(szRules, item->rulebuffer); } if (!IS_MAP_VALID(szNextMap)) { - Q_strcpy(szNextMap, szFirstMapInList); + Q_strlcpy(szNextMap, szFirstMapInList); } m_bGameOver = true; @@ -5077,17 +5069,7 @@ void CHalfLifeMultiplay::SendMOTDToClient(edict_t *client) while (pFileList && *pFileList && char_count < MAX_MOTD_LENGTH) { char chunk[MAX_MOTD_CHUNK + 1]; - - if (Q_strlen(pFileList) < sizeof(chunk)) - { - Q_strcpy(chunk, pFileList); - } - else - { - Q_strncpy(chunk, pFileList, sizeof(chunk) - 1); - // Q_strncpy doesn't always append the null terminator - chunk[sizeof(chunk) - 1] = '\0'; - } + Q_strlcpy(chunk, pFileList); char_count += Q_strlen(chunk); diff --git a/regamedll/dlls/observer.cpp b/regamedll/dlls/observer.cpp index 4b3521a6..3ccefc43 100644 --- a/regamedll/dlls/observer.cpp +++ b/regamedll/dlls/observer.cpp @@ -480,14 +480,14 @@ void EXT_FUNC CBasePlayer::__API_HOOK(Observer_SetMode)(int iMode) { #ifdef REGAMEDLL_FIXES m_hObserverTarget = Observer_IsValidTarget( ENTINDEX(m_hObserverTarget->edict()), forcecamera != CAMERA_MODE_SPEC_ANYONE ); -#else +#else CBasePlayer *pTarget = m_hObserverTarget; - if (pTarget == this - || !pTarget - || pTarget->has_disconnected - || pTarget->GetObserverMode() != OBS_NONE - || (pTarget->pev->effects & EF_NODRAW) + if (pTarget == this + || !pTarget + || pTarget->has_disconnected + || pTarget->GetObserverMode() != OBS_NONE + || (pTarget->pev->effects & EF_NODRAW) || (forcecamera != CAMERA_MODE_SPEC_ANYONE && pTarget->m_iTeam != m_iTeam)) m_hObserverTarget = nullptr; #endif @@ -534,7 +534,7 @@ void EXT_FUNC CBasePlayer::__API_HOOK(Observer_SetMode)(int iMode) // print spepctaor mode on client screen char modemsg[16]; - Q_sprintf(modemsg, "#Spec_Mode%i", pev->iuser1); + Q_snprintf(modemsg, sizeof(modemsg), "#Spec_Mode%i", pev->iuser1); ClientPrint(pev, HUD_PRINTCENTER, modemsg); m_iObserverLastMode = iMode; @@ -548,4 +548,4 @@ void EXT_FUNC CBasePlayer::__API_HOOK(Observer_Think)() Observer_HandleButtons(); Observer_CheckTarget(); Observer_CheckProperties(); -} \ No newline at end of file +} diff --git a/regamedll/dlls/player.cpp b/regamedll/dlls/player.cpp index c297d827..8ece842e 100644 --- a/regamedll/dlls/player.cpp +++ b/regamedll/dlls/player.cpp @@ -2556,11 +2556,19 @@ BOOL CBasePlayer::IsBombGuy() LINK_HOOK_CLASS_VOID_CHAIN(CBasePlayer, SetAnimation, (PLAYER_ANIM playerAnim), playerAnim) +int CBasePlayer::GetAnimDesired(const char *szAnim, AnimationType type) +{ + const char *refAnim = (type == ANIM_CROUCH && (pev->flags & FL_DUCKING)) ? "crouch_" : "ref_"; + + char szAnimConstruct[128]; + Q_snprintf(szAnimConstruct, sizeof(szAnimConstruct), "%s%s_%s", refAnim, szAnim, m_szAnimExtention); + return LookupSequence(szAnimConstruct); +} + void EXT_FUNC CBasePlayer::__API_HOOK(SetAnimation)(PLAYER_ANIM playerAnim) { int animDesired; float speed; - char szAnim[64]; int hopSeq; int leapSeq; @@ -2696,16 +2704,17 @@ void EXT_FUNC CBasePlayer::__API_HOOK(SetAnimation)(PLAYER_ANIM playerAnim) if (m_Activity == m_IdealActivity) return; + const char *refAnim; + switch (m_Activity) { - case ACT_RANGE_ATTACK1: Q_strcpy(szAnim, "ref_shoot_"); break; - case ACT_RANGE_ATTACK2: Q_strcpy(szAnim, "ref_shoot2_"); break; - case ACT_RELOAD: Q_strcpy(szAnim, "ref_reload_"); break; - default: Q_strcpy(szAnim, "ref_aim_"); break; + case ACT_RANGE_ATTACK1: refAnim = "shoot"; break; + case ACT_RANGE_ATTACK2: refAnim = "shoot2"; break; + case ACT_RELOAD: refAnim = "reload"; break; + default: refAnim = "aim"; break; } - Q_strcat(szAnim, m_szAnimExtention); - animDesired = LookupSequence(szAnim); + animDesired = GetAnimDesired(refAnim, ANIM_NORMAL); if (animDesired == -1) animDesired = 0; @@ -2727,13 +2736,7 @@ void EXT_FUNC CBasePlayer::__API_HOOK(SetAnimation)(PLAYER_ANIM playerAnim) { m_flLastFired = gpGlobals->time; - if (pev->flags & FL_DUCKING) - Q_strcpy(szAnim, "crouch_shoot_"); - else - Q_strcpy(szAnim, "ref_shoot_"); - - Q_strcat(szAnim, m_szAnimExtention); - animDesired = LookupSequence(szAnim); + animDesired = GetAnimDesired("shoot", ANIM_CROUCH); if (animDesired == -1) animDesired = 0; @@ -2748,13 +2751,7 @@ void EXT_FUNC CBasePlayer::__API_HOOK(SetAnimation)(PLAYER_ANIM playerAnim) { m_flLastFired = gpGlobals->time; - if (pev->flags & FL_DUCKING) - Q_strcpy(szAnim, "crouch_shoot2_"); - else - Q_strcpy(szAnim, "ref_shoot2_"); - - Q_strcat(szAnim, m_szAnimExtention); - animDesired = LookupSequence(szAnim); + animDesired = GetAnimDesired("shoot2", ANIM_CROUCH); if (animDesired == -1) animDesired = 0; @@ -2767,13 +2764,7 @@ void EXT_FUNC CBasePlayer::__API_HOOK(SetAnimation)(PLAYER_ANIM playerAnim) } case ACT_RELOAD: { - if (pev->flags & FL_DUCKING) - Q_strcpy(szAnim, "crouch_reload_"); - else - Q_strcpy(szAnim, "ref_reload_"); - - Q_strcat(szAnim, m_szAnimExtention); - animDesired = LookupSequence(szAnim); + animDesired = GetAnimDesired("reload", ANIM_CROUCH); if (animDesired == -1) animDesired = 0; @@ -2788,13 +2779,7 @@ void EXT_FUNC CBasePlayer::__API_HOOK(SetAnimation)(PLAYER_ANIM playerAnim) } case ACT_HOLDBOMB: { - if (pev->flags & FL_DUCKING) - Q_strcpy(szAnim, "crouch_aim_"); - else - Q_strcpy(szAnim, "ref_aim_"); - - Q_strcat(szAnim, m_szAnimExtention); - animDesired = LookupSequence(szAnim); + animDesired = GetAnimDesired("aim", ANIM_CROUCH); if (animDesired == -1) animDesired = 0; @@ -2811,13 +2796,7 @@ void EXT_FUNC CBasePlayer::__API_HOOK(SetAnimation)(PLAYER_ANIM playerAnim) { if (speed <= 135.0f || m_flLastFired + 4.0 >= gpGlobals->time) { - if (pev->flags & FL_DUCKING) - Q_strcpy(szAnim, "crouch_aim_"); - else - Q_strcpy(szAnim, "ref_aim_"); - - Q_strcat(szAnim, m_szAnimExtention); - animDesired = LookupSequence(szAnim); + animDesired = GetAnimDesired("aim", ANIM_CROUCH); if (animDesired == -1) animDesired = 0; @@ -2825,18 +2804,10 @@ void EXT_FUNC CBasePlayer::__API_HOOK(SetAnimation)(PLAYER_ANIM playerAnim) } else { - Q_strcpy(szAnim, "run_"); - Q_strcat(szAnim, m_szAnimExtention); - animDesired = LookupSequence(szAnim); + animDesired = GetAnimDesired("run", ANIM_NORMAL); if (animDesired == -1) { - if (pev->flags & FL_DUCKING) - Q_strcpy(szAnim, "crouch_aim_"); - else - Q_strcpy(szAnim, "ref_aim_"); - - Q_strcat(szAnim, m_szAnimExtention); - animDesired = LookupSequence(szAnim); + animDesired = GetAnimDesired("aim", ANIM_CROUCH); if (animDesired == -1) animDesired = 0; @@ -3086,20 +3057,9 @@ void EXT_FUNC CBasePlayer::__API_HOOK(SetAnimation)(PLAYER_ANIM playerAnim) { if (m_Activity != ACT_FLINCH && m_Activity != ACT_LARGE_FLINCH) { - Q_strcpy(szAnim, "run_"); - Q_strcat(szAnim, m_szAnimExtention); - - animDesired = LookupSequence(szAnim); + animDesired = GetAnimDesired("run", ANIM_NORMAL); if (animDesired == -1) - { - if (pev->flags & FL_DUCKING) - Q_strcpy(szAnim, "crouch_aim_"); - else - Q_strcpy(szAnim, "ref_aim_"); - - Q_strcat(szAnim, m_szAnimExtention); - animDesired = LookupSequence(szAnim); - } + animDesired = GetAnimDesired("aim", ANIM_CROUCH); else pev->gaitsequence = animDesired; @@ -5047,8 +5007,7 @@ void CBasePlayer::CheckSuitUpdate() { // play sentence number char sentence[MAX_SENTENCE_NAME + 1]; - Q_strcpy(sentence, "!"); - Q_strcat(sentence, gszallsentencenames[isentence]); + Q_snprintf(sentence, sizeof(sentence), "!%s", gszallsentencenames[isentence]); EMIT_SOUND_SUIT(ENT(pev), sentence); } else @@ -8074,7 +8033,7 @@ void CBasePlayer::UpdateStatusBar() char sbuf0[MAX_SBAR_STRING]; Q_memset(newSBarState, 0, sizeof(newSBarState)); - Q_strcpy(sbuf0, m_SbarString0); + Q_strlcpy(sbuf0, m_SbarString0); // Find an ID Target TraceResult tr; @@ -8104,9 +8063,9 @@ void CBasePlayer::UpdateStatusBar() if (sameTeam || GetObserverMode() != OBS_NONE) { if (playerid.value != PLAYERID_MODE_OFF || GetObserverMode() != OBS_NONE) - Q_strcpy(sbuf0, "1 %c1: %p2\n2 %h: %i3%%"); + Q_strlcpy(sbuf0, "1 %c1: %p2\n2 %h: %i3%%"); else - Q_strcpy(sbuf0, " "); + Q_strlcpy(sbuf0, " "); newSBarState[SBAR_ID_TARGETHEALTH] = int((pEntity->pev->health / pEntity->pev->max_health) * 100); @@ -8119,9 +8078,9 @@ void CBasePlayer::UpdateStatusBar() else if (GetObserverMode() == OBS_NONE) { if (playerid.value != PLAYERID_MODE_TEAMONLY && playerid.value != PLAYERID_MODE_OFF) - Q_strcpy(sbuf0, "1 %c1: %p2"); + Q_strlcpy(sbuf0, "1 %c1: %p2"); else - Q_strcpy(sbuf0, " "); + Q_strlcpy(sbuf0, " "); if (!(m_flDisplayHistory & DHF_ENEMY_SEEN)) { @@ -8135,9 +8094,9 @@ void CBasePlayer::UpdateStatusBar() else if (pEntity->Classify() == CLASS_HUMAN_PASSIVE) { if (playerid.value != PLAYERID_MODE_OFF || GetObserverMode() != OBS_NONE) - Q_strcpy(sbuf0, "1 %c1 %h: %i3%%"); + Q_strlcpy(sbuf0, "1 %c1 %h: %i3%%"); else - Q_strcpy(sbuf0, " "); + Q_strlcpy(sbuf0, " "); newSBarState[SBAR_ID_TARGETTYPE] = SBAR_TARGETTYPE_HOSTAGE; newSBarState[SBAR_ID_TARGETHEALTH] = int((pEntity->pev->health / pEntity->pev->max_health) * 100); @@ -8179,7 +8138,7 @@ void CBasePlayer::UpdateStatusBar() WRITE_STRING(sbuf0); MESSAGE_END(); - Q_strcpy(m_SbarString0, sbuf0); + Q_strlcpy(m_SbarString0, sbuf0); // make sure everything's resent bForceResend = true; @@ -9370,14 +9329,10 @@ void CBasePlayer::AddAutoBuyData(const char *str) { if (len > 0) { - Q_strncat(m_autoBuyString, " ", len); + Q_strlcat(m_autoBuyString, " "); } -#ifndef REGAMEDLL_FIXES - Q_strncat(m_autoBuyString, str, sizeof(m_autoBuyString) - Q_strlen(m_autoBuyString)); -#else - Q_strncat(m_autoBuyString, str, sizeof(m_autoBuyString) - Q_strlen(m_autoBuyString) - 1); -#endif + Q_strlcat(m_autoBuyString, str); } } @@ -9394,9 +9349,7 @@ void CBasePlayer::InitRebuyData(const char *str) m_rebuyString = nullptr; } - m_rebuyString = new char[Q_strlen(str) + 1]; - Q_strcpy(m_rebuyString, str); - m_rebuyString[Q_strlen(str)] = '\0'; + m_rebuyString = CloneString(str); } void CBasePlayer::AutoBuy() @@ -9424,7 +9377,7 @@ void CBasePlayer::AutoBuy() if (c) { - Q_strcpy(prioritizedString, c); + Q_strlcpy(prioritizedString, c); PrioritizeAutoBuyString(prioritizedString, m_autoBuyString); ParseAutoBuyString(prioritizedString, boughtPrimary, boughtSecondary); @@ -9434,7 +9387,7 @@ void CBasePlayer::AutoBuy() if (c) { - Q_strcpy(prioritizedString, c); + Q_strlcpy(prioritizedString, c); PrioritizeAutoBuyString(prioritizedString, m_autoBuyString); ParseAutoBuyString(prioritizedString, boughtPrimary, boughtSecondary); @@ -9582,11 +9535,11 @@ const char *CBasePlayer::PickPrimaryCareerTaskWeapon() CCareerTask *pTask = taskVector[i]; if (IsPrimaryWeaponId(pTask->GetWeaponId())) - Q_strncat(buf, WeaponIDToAlias(pTask->GetWeaponId()), sizeof(buf) - Q_strlen(buf) - 1); + Q_strlcat(buf, WeaponIDToAlias(pTask->GetWeaponId())); else - Q_strncat(buf, GetBuyStringForWeaponClass(pTask->GetWeaponClassId()), sizeof(buf) - Q_strlen(buf) - 1); + Q_strlcat(buf, GetBuyStringForWeaponClass(pTask->GetWeaponClassId())); - Q_strncat(buf, " ", sizeof(buf) - Q_strlen(buf) - 1); + Q_strlcat(buf, " "); } return buf; @@ -9657,11 +9610,11 @@ const char *CBasePlayer::PickSecondaryCareerTaskWeapon() CCareerTask *pTask = taskVector[i]; if (IsSecondaryWeaponId(pTask->GetWeaponId())) - Q_strncat(buf, WeaponIDToAlias(pTask->GetWeaponId()), sizeof(buf) - Q_strlen(buf) - 1); + Q_strlcat(buf, WeaponIDToAlias(pTask->GetWeaponId())); else - Q_strncat(buf, GetBuyStringForWeaponClass(pTask->GetWeaponClassId()), sizeof(buf) - Q_strlen(buf) - 1); + Q_strlcat(buf, GetBuyStringForWeaponClass(pTask->GetWeaponClassId())); - Q_strncat(buf, " ", sizeof(buf) - Q_strlen(buf) - 1); + Q_strlcat(buf, " "); } return buf; @@ -9710,7 +9663,7 @@ const char *CBasePlayer::PickGrenadeKillWeaponString() } // PostAutoBuyCommandProcessing - reorders the tokens in autobuyString based on the order of tokens in the priorityString. -void CBasePlayer::PrioritizeAutoBuyString(char *autobuyString, const char *priorityString) +void CBasePlayer::PrioritizeAutoBuyString(char (&autobuyString)[MAX_AUTOBUY_LENGTH], const char *priorityString) { char newString[MAX_AUTOBUY_LENGTH]; int newStringPos = 0; @@ -9783,7 +9736,7 @@ void CBasePlayer::PrioritizeAutoBuyString(char *autobuyString, const char *prior // terminate the string. Trailing spaces shouldn't matter. newString[newStringPos] = '\0'; - Q_sprintf(autobuyString, "%s", newString); + Q_snprintf(autobuyString, sizeof(autobuyString), "%s", newString); } void CBasePlayer::ParseAutoBuyString(const char *string, bool &boughtPrimary, bool &boughtSecondary) diff --git a/regamedll/dlls/player.h b/regamedll/dlls/player.h index 182c4a37..b13c2c82 100644 --- a/regamedll/dlls/player.h +++ b/regamedll/dlls/player.h @@ -522,7 +522,9 @@ public: void UpdatePlayerSound(); void DeathSound(); void SetAnimation(PLAYER_ANIM playerAnim); - void SetWeaponAnimType(const char *szExtention) { Q_strcpy(m_szAnimExtention, szExtention); } + enum AnimationType { ANIM_NORMAL, ANIM_CROUCH }; + int GetAnimDesired(const char *szAnim, AnimationType type); + void SetWeaponAnimType(const char *szExtention) { Q_strlcpy(m_szAnimExtention, szExtention); } void CheatImpulseCommands(int iImpulse); void StartDeathCam(); void StartObserver(Vector &vecPosition, Vector &vecViewAngle); @@ -602,7 +604,7 @@ public: void AddAutoBuyData(const char *str); void AutoBuy(); void ClientCommand(const char *cmd, const char *arg1 = nullptr, const char *arg2 = nullptr, const char *arg3 = nullptr); - void PrioritizeAutoBuyString(char *autobuyString, const char *priorityString); + void PrioritizeAutoBuyString(char (&autobuyString)[MAX_AUTOBUY_LENGTH], const char *priorityString); const char *PickPrimaryCareerTaskWeapon(); const char *PickSecondaryCareerTaskWeapon(); const char *PickFlashKillWeaponString(); diff --git a/regamedll/dlls/saverestore.cpp b/regamedll/dlls/saverestore.cpp index a3a55c7f..897452c4 100644 --- a/regamedll/dlls/saverestore.cpp +++ b/regamedll/dlls/saverestore.cpp @@ -962,8 +962,8 @@ void CGlobalState::EntityAdd(string_t globalname, string_t mapName, GLOBALESTATE pNewEntity->pNext = m_pList; m_pList = pNewEntity; - Q_strcpy(pNewEntity->name, STRING(globalname)); - Q_strcpy(pNewEntity->levelName, STRING(mapName)); + Q_strlcpy(pNewEntity->name, STRING(globalname)); + Q_strlcpy(pNewEntity->levelName, STRING(mapName)); pNewEntity->state = state; m_listCount++; @@ -1068,7 +1068,7 @@ void CGlobalState::EntityUpdate(string_t globalname, string_t mapname) globalentity_t *pEnt = Find(globalname); if (pEnt) { - Q_strcpy(pEnt->levelName, STRING(mapname)); + Q_strlcpy(pEnt->levelName, STRING(mapname)); } } diff --git a/regamedll/dlls/skill.cpp b/regamedll/dlls/skill.cpp index 87fd4468..3c0bfe0d 100644 --- a/regamedll/dlls/skill.cpp +++ b/regamedll/dlls/skill.cpp @@ -10,7 +10,7 @@ NOXREF float GetSkillCvar(char *pName) float flValue; char szBuffer[64]; - iCount = Q_sprintf(szBuffer, "%s%d", pName, gSkillData.iSkillLevel); + iCount = Q_snprintf(szBuffer, sizeof(szBuffer), "%s%d", pName, gSkillData.iSkillLevel); flValue = CVAR_GET_FLOAT(szBuffer); if (flValue <= 0.0f) diff --git a/regamedll/dlls/sound.cpp b/regamedll/dlls/sound.cpp index baf5353c..a4dc0a7d 100644 --- a/regamedll/dlls/sound.cpp +++ b/regamedll/dlls/sound.cpp @@ -1040,11 +1040,10 @@ void USENTENCEG_InitLRU(unsigned char *plru, int count) // ipick is passed in as the requested sentence ordinal. // ipick 'next' is returned. // return of -1 indicates an error. -int USENTENCEG_PickSequential(int isentenceg, char *szfound, int ipick, int freset) +int USENTENCEG_PickSequential(int isentenceg, char (&szfound)[64], int ipick, int freset) { char *szgroupname; unsigned char count; - char sznum[12]; if (!fSentencesInit) return -1; @@ -1061,10 +1060,7 @@ int USENTENCEG_PickSequential(int isentenceg, char *szfound, int ipick, int fres if (ipick >= count) ipick = count - 1; - Q_strcpy(szfound, "!"); - Q_strcat(szfound, szgroupname); - Q_snprintf(sznum, sizeof(sznum), "%d", ipick); - Q_strcat(szfound, sznum); + Q_snprintf(szfound, sizeof(szfound), "!%s%d", szgroupname, ipick); if (ipick >= count) { @@ -1084,13 +1080,12 @@ int USENTENCEG_PickSequential(int isentenceg, char *szfound, int ipick, int fres // rest of the lru filled with -1. The first integer in the lru is // actually the size of the list. Returns ipick, the ordinal // of the picked sentence within the group. -int USENTENCEG_Pick(int isentenceg, char *szfound) +int USENTENCEG_Pick(int isentenceg, char (&szfound)[64]) { char *szgroupname; unsigned char *plru; unsigned char i; unsigned char count; - char sznum[12]; unsigned char ipick = 0xFF; BOOL ffound = FALSE; @@ -1119,11 +1114,7 @@ int USENTENCEG_Pick(int isentenceg, char *szfound) if (ffound) { - Q_strcpy(szfound, "!"); - Q_strcat(szfound, szgroupname); - Q_snprintf(sznum, sizeof(sznum), "%d", ipick); - Q_strcat(szfound, sznum); - + Q_snprintf(szfound, sizeof(szfound), "!%s%d", szgroupname, ipick); return ipick; } else @@ -1168,8 +1159,6 @@ int SENTENCEG_PlayRndI(edict_t *entity, int isentenceg, float volume, float atte if (!fSentencesInit) return -1; - name[0] = '\0'; - ipick = USENTENCEG_Pick(isentenceg, name); #ifndef REGAMEDLL_FIXES @@ -1194,8 +1183,6 @@ int SENTENCEG_PlayRndSz(edict_t *entity, const char *szgroupname, float volume, if (!fSentencesInit) return -1; - name[0] = '\0'; - isentenceg = SENTENCEG_GetIndex(szgroupname); if (isentenceg < 0) { @@ -1223,8 +1210,6 @@ int SENTENCEG_PlaySequentialSz(edict_t *entity, const char *szgroupname, float v if (!fSentencesInit) return -1; - name[0] = '\0'; - isentenceg = SENTENCEG_GetIndex(szgroupname); if (isentenceg < 0) return -1; @@ -1323,7 +1308,7 @@ void SENTENCEG_Init() ALERT(at_warning, "Sentence %s longer than %d letters\n", pString, MAX_SENTENCE_NAME - 1); } - Q_strcpy(gszallsentencenames[gcallsentences++], pString); + Q_strlcpy(gszallsentencenames[gcallsentences++], pString); if (--j <= i) continue; @@ -1354,10 +1339,10 @@ void SENTENCEG_Init() break; } - Q_strcpy(rgsentenceg[isentencegs].szgroupname, &(buffer[i])); + Q_strlcpy(rgsentenceg[isentencegs].szgroupname, &(buffer[i])); rgsentenceg[isentencegs].count = 1; - Q_strcpy(szgroup, &(buffer[i])); + Q_strlcpy(szgroup, &(buffer[i])); continue; } @@ -1385,9 +1370,8 @@ void SENTENCEG_Init() } // convert sentence (sample) name to !sentencenum, return !sentencenum -int SENTENCEG_Lookup(const char *sample, char *sentencenum) +int SENTENCEG_Lookup(const char *sample, char (&sentencenum)[32]) { - char sznum[12]; int i; // this is a sentence name; lookup sentence number @@ -1398,9 +1382,7 @@ int SENTENCEG_Lookup(const char *sample, char *sentencenum) { if (sentencenum) { - Q_strcpy(sentencenum, "!"); - Q_snprintf(sznum, sizeof(sznum), "%d", i); - Q_strcat(sentencenum, sznum); + Q_snprintf(sentencenum, sizeof(sentencenum), "!%d", i); } return i; @@ -1580,7 +1562,7 @@ void TEXTURETYPE_Init() j = Q_min(j, MAX_TEXTURENAME_LENGHT - 1 + i); buffer[j] = '\0'; - Q_strcpy(&(grgszTextureName[gcTextures++][0]), &(buffer[i])); + Q_strlcpy(grgszTextureName[gcTextures++], &(buffer[i])); } FREE_FILE(pMemFile); @@ -1616,7 +1598,7 @@ float TEXTURETYPE_PlaySound(TraceResult *ptr, Vector vecSrc, Vector vecEnd, int char chTextureType; float fvol; float fvolbar; - char szBuffer[64]; + char szBuffer[MAX_TEXTURENAME_LENGHT]; const char *pTextureName; float rgfl1[3]; float rgfl2[3]; @@ -1666,8 +1648,7 @@ float TEXTURETYPE_PlaySound(TraceResult *ptr, Vector vecSrc, Vector vecEnd, int pTextureName++; // '}}' - Q_strcpy(szBuffer, pTextureName); - szBuffer[MAX_TEXTURENAME_LENGHT - 1] = '\0'; + Q_strlcpy(szBuffer, pTextureName); // get texture type chTextureType = TEXTURETYPE_Find(szBuffer); diff --git a/regamedll/dlls/sound.h b/regamedll/dlls/sound.h index d2f0b41a..b4e716fb 100644 --- a/regamedll/dlls/sound.h +++ b/regamedll/dlls/sound.h @@ -170,15 +170,13 @@ public: BOOL FEnvSoundInRange(entvars_t *pev, entvars_t *pevTarget, float *pflRange); void USENTENCEG_InitLRU(unsigned char *plru, int count); -int USENTENCEG_PickSequential(int isentenceg, char *szfound, int ipick, int freset); -int USENTENCEG_Pick(int isentenceg, char *szfound); int SENTENCEG_GetIndex(const char *szgroupname); int SENTENCEG_PlayRndI(edict_t *entity, int isentenceg, float volume, float attenuation, int flags, int pitch); int SENTENCEG_PlayRndSz(edict_t *entity, const char *szgroupname, float volume, float attenuation, int flags, int pitch); int SENTENCEG_PlaySequentialSz(edict_t *entity, const char *szgroupname, float volume, float attenuation, int flags, int pitch, int ipick, int freset); void SENTENCEG_Stop(edict_t *entity, int isentenceg, int ipick); void SENTENCEG_Init(); -int SENTENCEG_Lookup(const char *sample, char *sentencenum); +int SENTENCEG_Lookup(const char *sample, char (&sentencenum)[32]); void EMIT_SOUND_DYN(edict_t *entity, int channel, const char *sample, float volume, float attenuation, int flags, int pitch); void EMIT_SOUND_SUIT(edict_t *entity, const char *sample); void EMIT_GROUPID_SUIT(edict_t *entity, int isentenceg); diff --git a/regamedll/dlls/triggers.cpp b/regamedll/dlls/triggers.cpp index 82bf6907..1bfd85b9 100644 --- a/regamedll/dlls/triggers.cpp +++ b/regamedll/dlls/triggers.cpp @@ -645,7 +645,7 @@ void PlayCDTrack(edict_t *pClient, int iTrack) CLIENT_COMMAND(pClient, UTIL_VarArgs("mp3 play %s\n", g_szMP3trackFileMap[iTrack])); #else char string[64]; - Q_sprintf(string, "cd play %3d\n", iTrack); + Q_snprintf(string, sizeof(string), "cd play %3d\n", iTrack); CLIENT_COMMAND(pClient, string); #endif } @@ -1214,7 +1214,7 @@ void CChangeLevel::KeyValue(KeyValueData *pkvd) ALERT(at_error, "Map name '%s' too long (32 chars)\n", pkvd->szValue); } - Q_strcpy(m_szMapName, pkvd->szValue); + Q_strlcpy(m_szMapName, pkvd->szValue); pkvd->fHandled = TRUE; } else if (FStrEq(pkvd->szKeyName, "landmark")) @@ -1224,7 +1224,7 @@ void CChangeLevel::KeyValue(KeyValueData *pkvd) ALERT(at_error, "Landmark name '%s' too long (32 chars)\n", pkvd->szValue); } - Q_strcpy(m_szLandmarkName, pkvd->szValue); + Q_strlcpy(m_szLandmarkName, pkvd->szValue); pkvd->fHandled = TRUE; } else if (FStrEq(pkvd->szKeyName, "changetarget")) @@ -1356,7 +1356,7 @@ void CChangeLevel::ChangeLevelNow(CBaseEntity *pActivator) } // This object will get removed in the call to CHANGE_LEVEL, copy the params into "safe" memory - Q_strcpy(st_szNextMap, m_szMapName); + Q_strlcpy(st_szNextMap, m_szMapName); m_hActivator = pActivator; SUB_UseTargets(pActivator, USE_TOGGLE, 0); @@ -1369,7 +1369,7 @@ void CChangeLevel::ChangeLevelNow(CBaseEntity *pActivator) if (!FNullEnt(pentLandmark)) { - Q_strcpy(st_szNextSpot, m_szLandmarkName); + Q_strlcpy(st_szNextSpot, m_szLandmarkName); gpGlobals->vecLandmarkOffset = VARS(pentLandmark)->origin; } @@ -1415,8 +1415,8 @@ int CChangeLevel::AddTransitionToList(LEVELLIST *pLevelList, int listCount, cons } } - Q_strcpy(pLevelList[listCount].mapName, pMapName); - Q_strcpy(pLevelList[listCount].landmarkName, pLandmarkName); + Q_strlcpy(pLevelList[listCount].mapName, pMapName); + Q_strlcpy(pLevelList[listCount].landmarkName, pLandmarkName); pLevelList[listCount].pentLandmark = pentLandmark; pLevelList[listCount].vecLandmarkOrigin = VARS(pentLandmark)->origin; @@ -1591,12 +1591,12 @@ NOXREF void NextLevel() { gpGlobals->mapname = ALLOC_STRING("start"); pChange = GetClassPtr((CChangeLevel *)nullptr); - Q_strcpy(pChange->m_szMapName, "start"); + Q_strlcpy(pChange->m_szMapName, "start"); } else pChange = GetClassPtr((CChangeLevel *)VARS(pent)); - Q_strcpy(st_szNextMap, pChange->m_szMapName); + Q_strlcpy(st_szNextMap, pChange->m_szMapName); g_pGameRules->SetGameOver(); if (pChange->pev->nextthink < gpGlobals->time) diff --git a/regamedll/dlls/tutor_base_tutor.cpp b/regamedll/dlls/tutor_base_tutor.cpp index 2268d18b..a44ef472 100644 --- a/regamedll/dlls/tutor_base_tutor.cpp +++ b/regamedll/dlls/tutor_base_tutor.cpp @@ -68,12 +68,10 @@ void TutorMessageEvent::AddParameter(char *str) TutorMessageEventParam *param = new TutorMessageEventParam; param->m_next = nullptr; - param->m_data = new char[Q_strlen(str) + 1]; + param->m_data = CloneString(str); if (param->m_data) { - Q_strcpy(param->m_data, str); - param->m_data[Q_strlen(str)] = '\0'; m_numParameters++; if (m_paramList) @@ -101,11 +99,7 @@ char *TutorMessageEvent::GetNextParameter(char *buf, int buflen) m_numParameters--; m_paramList = param->m_next; - Q_strncpy(buf, param->m_data, buflen); - -#ifdef REGAMEDLL_FIXES - buf[buflen - 1] = '\0'; -#endif + Q_strlcpy(buf, param->m_data, buflen); delete param; return buf; diff --git a/regamedll/dlls/tutor_cs_tutor.cpp b/regamedll/dlls/tutor_cs_tutor.cpp index 18e5d2b6..aa6bdd4b 100644 --- a/regamedll/dlls/tutor_cs_tutor.cpp +++ b/regamedll/dlls/tutor_cs_tutor.cpp @@ -213,7 +213,7 @@ void ParseMessageParameters(char *&messageData, TutorMessage *ret) if (!Q_stricmp(token, "String")) { messageData = SharedParse((char *)messageData); - ret->m_text = Q_strdup(SharedGetToken()); + ret->m_text = CloneString(SharedGetToken()); } else if (!Q_stricmp(token, "Duration")) { @@ -832,7 +832,7 @@ TutorMessageEvent *CCSTutor::CreateTutorMessageEvent(TutorMessageID mid, CBaseEn { numtasks = TheCareerTasks->GetNumRemainingTasks(); } - Q_sprintf(numLeftStr, "%d", numtasks); + Q_snprintf(numLeftStr, sizeof(numLeftStr), "%d", numtasks); event->AddParameter(numLeftStr); break; } @@ -2820,8 +2820,7 @@ void CCSTutor::ConstructRecentDeathsList(TeamName team, char *buf, int buflen, T if (!buf || !buflen) return; - char scratch[32]; - buf[0] = '\0'; + int len = 0; for (int i = 1; i <= gpGlobals->maxClients; i++) { @@ -2837,10 +2836,7 @@ void CCSTutor::ConstructRecentDeathsList(TeamName team, char *buf, int buflen, T if (pPlayer->m_iTeam != team) continue; - Q_strcat(buf, " %n"); - Q_sprintf(scratch, "%d\n", i); - Q_strcat(buf, scratch); - + len += Q_snprintf(&buf[len], buflen - len, " %%n%d\n", i); m_playerDeathInfo[i].m_event = event; } } diff --git a/regamedll/dlls/util.cpp b/regamedll/dlls/util.cpp index 7841b006..de615946 100644 --- a/regamedll/dlls/util.cpp +++ b/regamedll/dlls/util.cpp @@ -690,10 +690,7 @@ void UTIL_Log(const char *fmt, ...) Q_vsnprintf(string, sizeof(string), fmt, ap); va_end(ap); - if (Q_strlen(string) < sizeof(string) - 2) - Q_strcat(string, "\n"); - else - string[Q_strlen(string) - 1] = '\n'; + Q_strlcat(string, "\n"); FILE *fp = fopen("regamedll.log", "at"); if (fp) @@ -717,10 +714,7 @@ void UTIL_ServerPrint(const char *fmt, ...) Q_vsnprintf(string, sizeof(string), fmt, ap); va_end(ap); - if (Q_strlen(string) < sizeof(string) - 2) - Q_strcat(string, "\n"); - else - string[Q_strlen(string) - 1] = '\n'; + Q_strlcat(string, "\n"); SERVER_PRINT(string); } @@ -738,10 +732,7 @@ void UTIL_PrintConsole(edict_t *pEdict, const char *fmt, ...) Q_vsnprintf(string, sizeof(string), fmt, ap); va_end(ap); - if (Q_strlen(string) < sizeof(string) - 2) - Q_strcat(string, "\n"); - else - string[Q_strlen(string) - 1] = '\n'; + Q_strlcat(string, "\n"); ClientPrint(pEntity->pev, HUD_PRINTCONSOLE, string); } @@ -759,10 +750,7 @@ void UTIL_SayText(edict_t *pEdict, const char *fmt, ...) Q_vsnprintf(string, sizeof(string), fmt, ap); va_end(ap); - if (Q_strlen(string) < sizeof(string) - 2) - Q_strcat(string, "\n"); - else - string[Q_strlen(string) - 1] = '\n'; + Q_strlcat(string, "\n"); MESSAGE_BEGIN(MSG_ONE, gmsgSayText, nullptr, pEntity->edict()); WRITE_BYTE(pEntity->entindex()); @@ -781,28 +769,28 @@ void UTIL_SayTextAll(const char *pText, CBaseEntity *pEntity) char *UTIL_dtos1(int d) { static char buf[12]; - Q_sprintf(buf, "%d", d); + Q_snprintf(buf, sizeof(buf), "%d", d); return buf; } char *UTIL_dtos2(int d) { static char buf[12]; - Q_sprintf(buf, "%d", d); + Q_snprintf(buf, sizeof(buf), "%d", d); return buf; } NOXREF char *UTIL_dtos3(int d) { static char buf[12]; - Q_sprintf(buf, "%d", d); + Q_snprintf(buf, sizeof(buf), "%d", d); return buf; } NOXREF char *UTIL_dtos4(int d) { static char buf[12]; - Q_sprintf(buf, "%d", d); + Q_snprintf(buf, sizeof(buf), "%d", d); return buf; } @@ -991,7 +979,7 @@ char *UTIL_VarArgs(char *format, ...) static char string[1024]; va_start(argptr, format); - vsprintf(string, format, argptr); + Q_vsnprintf(string, sizeof(string), format, argptr); va_end(argptr); return string; @@ -1561,7 +1549,7 @@ void UTIL_LogPrintf(const char *fmt, ...) static char string[1024]; va_start(argptr, fmt); - vsprintf(string, fmt, argptr); + Q_vsnprintf(string, sizeof(string), fmt, argptr); va_end(argptr); ALERT(at_logged, "%s", string); @@ -1580,7 +1568,7 @@ char UTIL_TextureHit(TraceResult *ptr, Vector vecSrc, Vector vecEnd) float rgfl1[3]; float rgfl2[3]; const char *pTextureName; - char szbuffer[64]; + char szbuffer[MAX_TEXTURENAME_LENGHT]; CBaseEntity *pEntity = CBaseEntity::Instance(ptr->pHit); #ifdef REGAMEDLL_FIXES @@ -1606,8 +1594,8 @@ char UTIL_TextureHit(TraceResult *ptr, Vector vecSrc, Vector vecEnd) if (*pTextureName == '{' || *pTextureName == '!' || *pTextureName == '~' || *pTextureName == ' ') pTextureName++; - Q_strcpy(szbuffer, pTextureName); - szbuffer[16] = '\0'; + Q_strlcpy(szbuffer, pTextureName); + chTextureType = TEXTURETYPE_Find(szbuffer); } else diff --git a/regamedll/dlls/weapons.cpp b/regamedll/dlls/weapons.cpp index 087f45ee..47a77c6f 100644 --- a/regamedll/dlls/weapons.cpp +++ b/regamedll/dlls/weapons.cpp @@ -652,11 +652,11 @@ void CBasePlayerWeapon::SetPlayerShieldAnim() if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) { - Q_strcpy(m_pPlayer->m_szAnimExtention, "shield"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shield"); } else { - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgun"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgun"); } } @@ -666,7 +666,7 @@ void CBasePlayerWeapon::ResetPlayerShieldAnim() { if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) { - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgun"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgun"); } } } @@ -697,7 +697,7 @@ bool CBasePlayerWeapon::ShieldSecondaryFire(int iUpAnim, int iDownAnim) { m_iWeaponState &= ~WPNSTATE_SHIELD_DRAWN; SendWeaponAnim(iDownAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgun"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgun"); m_fMaxSpeed = 250.0f; m_pPlayer->m_bShieldDrawn = false; } @@ -705,7 +705,7 @@ bool CBasePlayerWeapon::ShieldSecondaryFire(int iUpAnim, int iDownAnim) { m_iWeaponState |= WPNSTATE_SHIELD_DRAWN; SendWeaponAnim(iUpAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shielded"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shielded"); m_fMaxSpeed = 180.0f; m_pPlayer->m_bShieldDrawn = true; } @@ -728,7 +728,7 @@ void EXT_FUNC CBasePlayerWeapon::__API_HOOK(KickBack)(float up_base, float later real_t flKickUp = up_base; float flKickLateral = lateral_base; - if (m_iShotsFired > 1) // consider == 0 case + if (m_iShotsFired > 1) // consider == 0 case { flKickUp += m_iShotsFired * up_modifier; flKickLateral += m_iShotsFired * lateral_modifier; @@ -1491,7 +1491,7 @@ BOOL EXT_FUNC CBasePlayerWeapon::__API_HOOK(DefaultDeploy)(char *szViewModel, ch m_pPlayer->pev->weaponmodel = MAKE_STRING(szWeaponModel); #endif model_name = m_pPlayer->pev->viewmodel; - Q_strcpy(m_pPlayer->m_szAnimExtention, szAnimExt); + Q_strlcpy(m_pPlayer->m_szAnimExtention, szAnimExt); SendWeaponAnim(iAnim, skiplocal); m_pPlayer->m_flNextAttack = 0.75f; @@ -1671,7 +1671,7 @@ void CBasePlayerWeapon::Holster(int skiplocal) m_fInReload = FALSE; m_pPlayer->pev->viewmodel = 0; m_pPlayer->pev->weaponmodel = 0; - + #ifdef REGAMEDLL_FIXES m_fInSpecialReload = 0; #endif diff --git a/regamedll/dlls/world.cpp b/regamedll/dlls/world.cpp index 55147af0..cc4a3de5 100644 --- a/regamedll/dlls/world.cpp +++ b/regamedll/dlls/world.cpp @@ -216,7 +216,7 @@ void CWorld::Spawn() Precache(); g_szMapBriefingText[0] = '\0'; - Q_sprintf(szMapBriefingFile, "maps/%s.txt", STRING(gpGlobals->mapname)); + Q_snprintf(szMapBriefingFile, sizeof(szMapBriefingFile), "maps/%s.txt", STRING(gpGlobals->mapname)); int flength = 0; char *pFile = (char *)LOAD_FILE_FOR_ME(szMapBriefingFile, &flength); diff --git a/regamedll/dlls/wpn_shared/wpn_flashbang.cpp b/regamedll/dlls/wpn_shared/wpn_flashbang.cpp index 0443353e..bbebb8eb 100644 --- a/regamedll/dlls/wpn_shared/wpn_flashbang.cpp +++ b/regamedll/dlls/wpn_shared/wpn_flashbang.cpp @@ -114,7 +114,7 @@ bool CFlashbang::ShieldSecondaryFire(int iUpAnim, int iDownAnim) m_iWeaponState &= ~WPNSTATE_SHIELD_DRAWN; SendWeaponAnim(iDownAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); m_fMaxSpeed = FLASHBANG_MAX_SPEED; m_pPlayer->m_bShieldDrawn = false; @@ -124,7 +124,7 @@ bool CFlashbang::ShieldSecondaryFire(int iUpAnim, int iDownAnim) m_iWeaponState |= WPNSTATE_SHIELD_DRAWN; SendWeaponAnim(iUpAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shielded"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shielded"); m_fMaxSpeed = FLASHBANG_MAX_SPEED_SHIELD; m_pPlayer->m_bShieldDrawn = true; @@ -151,9 +151,9 @@ void CFlashbang::SetPlayerShieldAnim() return; if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) - Q_strcpy(m_pPlayer->m_szAnimExtention, "shield"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shield"); else - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); } void CFlashbang::ResetPlayerShieldAnim() @@ -163,7 +163,7 @@ void CFlashbang::ResetPlayerShieldAnim() if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) { - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); } } diff --git a/regamedll/dlls/wpn_shared/wpn_hegrenade.cpp b/regamedll/dlls/wpn_shared/wpn_hegrenade.cpp index 49d8eb7a..ca2c63ad 100644 --- a/regamedll/dlls/wpn_shared/wpn_hegrenade.cpp +++ b/regamedll/dlls/wpn_shared/wpn_hegrenade.cpp @@ -117,7 +117,7 @@ bool CHEGrenade::ShieldSecondaryFire(int iUpAnim, int iDownAnim) { m_iWeaponState &= ~WPNSTATE_SHIELD_DRAWN; SendWeaponAnim(iDownAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); m_fMaxSpeed = HEGRENADE_MAX_SPEED; m_pPlayer->m_bShieldDrawn = false; @@ -126,7 +126,7 @@ bool CHEGrenade::ShieldSecondaryFire(int iUpAnim, int iDownAnim) { m_iWeaponState |= WPNSTATE_SHIELD_DRAWN; SendWeaponAnim(iUpAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shielded"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shielded"); m_fMaxSpeed = HEGRENADE_MAX_SPEED_SHIELD; m_pPlayer->m_bShieldDrawn = true; @@ -153,9 +153,9 @@ void CHEGrenade::SetPlayerShieldAnim() return; if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) - Q_strcpy(m_pPlayer->m_szAnimExtention, "shield"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shield"); else - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); } void CHEGrenade::ResetPlayerShieldAnim() @@ -165,7 +165,7 @@ void CHEGrenade::ResetPlayerShieldAnim() if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) { - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); } } diff --git a/regamedll/dlls/wpn_shared/wpn_knife.cpp b/regamedll/dlls/wpn_shared/wpn_knife.cpp index fcbf2d94..43a2f28f 100644 --- a/regamedll/dlls/wpn_shared/wpn_knife.cpp +++ b/regamedll/dlls/wpn_shared/wpn_knife.cpp @@ -180,7 +180,7 @@ void CKnife::SetPlayerShieldAnim() if (!m_pPlayer->HasShield()) return; - Q_strcpy(m_pPlayer->m_szAnimExtention, (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) != 0 ? "shield" : "shieldknife"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) != 0 ? "shield" : "shieldknife"); } void CKnife::ResetPlayerShieldAnim() @@ -190,7 +190,7 @@ void CKnife::ResetPlayerShieldAnim() if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) { - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldknife"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldknife"); } } @@ -207,7 +207,7 @@ bool CKnife::ShieldSecondaryFire(int iUpAnim, int iDownAnim) SendWeaponAnim(iDownAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldknife"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldknife"); m_fMaxSpeed = KNIFE_MAX_SPEED; m_pPlayer->m_bShieldDrawn = false; @@ -217,7 +217,7 @@ bool CKnife::ShieldSecondaryFire(int iUpAnim, int iDownAnim) m_iWeaponState |= WPNSTATE_SHIELD_DRAWN; SendWeaponAnim(iUpAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shielded"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shielded"); m_fMaxSpeed = KNIFE_MAX_SPEED_SHIELD; m_pPlayer->m_bShieldDrawn = true; @@ -339,14 +339,14 @@ BOOL CKnife::Swing(BOOL fFirst) m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 2.0f; // play wiff or swish sound - EMIT_SOUND_DYN(m_pPlayer->edict(), - CHAN_WEAPON, - RANDOM_LONG(0, 1) ? + EMIT_SOUND_DYN(m_pPlayer->edict(), + CHAN_WEAPON, + RANDOM_LONG(0, 1) ? "weapons/knife_slash1.wav" : - "weapons/knife_slash2.wav", - VOL_NORM, - ATTN_NORM, - 0, + "weapons/knife_slash2.wav", + VOL_NORM, + ATTN_NORM, + 0, 94); // player "shoot" animation @@ -390,10 +390,10 @@ BOOL CKnife::Swing(BOOL fFirst) m_pPlayer->SetAnimation(PLAYER_ATTACK1); ClearMultiDamage(); - pEntity->TraceAttack(m_pPlayer->pev, + pEntity->TraceAttack(m_pPlayer->pev, KnifeSwingDamage(m_flNextPrimaryAttack + 0.4f < UTIL_WeaponTimeBase()), - gpGlobals->v_forward, - &tr, + gpGlobals->v_forward, + &tr, (DMG_NEVERGIB | DMG_BULLET)); ApplyMultiDamage(m_pPlayer->pev, m_pPlayer->pev); @@ -402,7 +402,7 @@ BOOL CKnife::Swing(BOOL fFirst) if (pEntity) // -V595 #endif { - if (pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE + if (pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE #ifdef REGAMEDLL_FIXES && pEntity->Classify() != CLASS_VEHICLE #endif @@ -518,14 +518,14 @@ BOOL CKnife::Stab(BOOL fFirst) m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 1.0f; // play wiff or swish sound - EMIT_SOUND_DYN(m_pPlayer->edict(), - CHAN_WEAPON, - RANDOM_LONG(0, 1) ? - "weapons/knife_slash1.wav" : - "weapons/knife_slash2.wav", - VOL_NORM, - ATTN_NORM, - 0, + EMIT_SOUND_DYN(m_pPlayer->edict(), + CHAN_WEAPON, + RANDOM_LONG(0, 1) ? + "weapons/knife_slash1.wav" : + "weapons/knife_slash2.wav", + VOL_NORM, + ATTN_NORM, + 0, 94); // player "shoot" animation @@ -586,7 +586,7 @@ BOOL CKnife::Stab(BOOL fFirst) if (pEntity) // -V595 #endif { - if (pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE + if (pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE #ifdef REGAMEDLL_FIXES && pEntity->Classify() != CLASS_VEHICLE #endif diff --git a/regamedll/dlls/wpn_shared/wpn_m4a1.cpp b/regamedll/dlls/wpn_shared/wpn_m4a1.cpp index 89ea81b5..e9bb062e 100644 --- a/regamedll/dlls/wpn_shared/wpn_m4a1.cpp +++ b/regamedll/dlls/wpn_shared/wpn_m4a1.cpp @@ -82,13 +82,13 @@ void CM4A1::SecondaryAttack() { m_iWeaponState &= ~WPNSTATE_M4A1_SILENCED; SendWeaponAnim(M4A1_DETACH_SILENCER, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "rifle"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "rifle"); } else { m_iWeaponState |= WPNSTATE_M4A1_SILENCED; SendWeaponAnim(M4A1_ATTACH_SILENCER, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "rifle"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "rifle"); } m_flTimeWeaponIdle = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 2.0f; diff --git a/regamedll/dlls/wpn_shared/wpn_smokegrenade.cpp b/regamedll/dlls/wpn_shared/wpn_smokegrenade.cpp index ae13158b..56c5e27b 100644 --- a/regamedll/dlls/wpn_shared/wpn_smokegrenade.cpp +++ b/regamedll/dlls/wpn_shared/wpn_smokegrenade.cpp @@ -117,7 +117,7 @@ bool CSmokeGrenade::ShieldSecondaryFire(int iUpAnim, int iDownAnim) m_iWeaponState &= ~WPNSTATE_SHIELD_DRAWN; SendWeaponAnim(iDownAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); m_fMaxSpeed = SMOKEGRENADE_MAX_SPEED; m_pPlayer->m_bShieldDrawn = false; @@ -127,7 +127,7 @@ bool CSmokeGrenade::ShieldSecondaryFire(int iUpAnim, int iDownAnim) m_iWeaponState |= WPNSTATE_SHIELD_DRAWN; SendWeaponAnim(iUpAnim, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "shielded"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shielded"); m_fMaxSpeed = SMOKEGRENADE_MAX_SPEED_SHIELD; m_pPlayer->m_bShieldDrawn = true; @@ -154,9 +154,9 @@ void CSmokeGrenade::SetPlayerShieldAnim() return; if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) - Q_strcpy(m_pPlayer->m_szAnimExtention, "shield"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shield"); else - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); } void CSmokeGrenade::ResetPlayerShieldAnim() @@ -166,7 +166,7 @@ void CSmokeGrenade::ResetPlayerShieldAnim() if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN) { - Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "shieldgren"); } } diff --git a/regamedll/dlls/wpn_shared/wpn_usp.cpp b/regamedll/dlls/wpn_shared/wpn_usp.cpp index 5a474f65..de823f30 100644 --- a/regamedll/dlls/wpn_shared/wpn_usp.cpp +++ b/regamedll/dlls/wpn_shared/wpn_usp.cpp @@ -98,14 +98,14 @@ void CUSP::SecondaryAttack() m_iWeaponState &= ~WPNSTATE_USP_SILENCED; SendWeaponAnim(USP_DETACH_SILENCER, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "onehanded"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "onehanded"); } else { m_iWeaponState |= WPNSTATE_USP_SILENCED; SendWeaponAnim(USP_ATTACH_SILENCER, UseDecrement() != FALSE); - Q_strcpy(m_pPlayer->m_szAnimExtention, "onehanded"); + Q_strlcpy(m_pPlayer->m_szAnimExtention, "onehanded"); } m_flNextSecondaryAttack = m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + USP_ADJUST_SIL_TIME; diff --git a/regamedll/game_shared/bot/bot.cpp b/regamedll/game_shared/bot/bot.cpp index 0f19e81f..52d1f4db 100644 --- a/regamedll/game_shared/bot/bot.cpp +++ b/regamedll/game_shared/bot/bot.cpp @@ -486,11 +486,11 @@ NOXREF void CBot::Print(char *format, ...) const char buffer[1024]; // prefix the message with the bot's name - Q_sprintf(buffer, "%s: ", STRING(pev->netname)); + Q_snprintf(buffer, sizeof(buffer), "%s: ", STRING(pev->netname)); SERVER_PRINT(buffer); va_start(varg, format); - vsprintf(buffer, format, varg); + Q_vsnprintf(buffer, sizeof(buffer), format, varg); va_end(varg); SERVER_PRINT(buffer); @@ -509,12 +509,12 @@ void CBot::PrintIfWatched(char *format, ...) const // prefix the message with the bot's name (this can be NULL if bot was just added) const char *name = pev ? STRING(pev->netname) : "(NULL pev)"; - Q_sprintf(buffer, "%s: ", name ? name : "(NULL netname)"); + Q_snprintf(buffer, sizeof(buffer), "%s: ", name ? name : "(NULL netname)"); SERVER_PRINT(buffer); va_start(varg, format); - vsprintf(buffer, format, varg); + Q_vsnprintf(buffer, sizeof(buffer), format, varg); va_end(varg); SERVER_PRINT(buffer); diff --git a/regamedll/game_shared/bot/bot_manager.cpp b/regamedll/game_shared/bot/bot_manager.cpp index 7ccd3281..3fa121ba 100644 --- a/regamedll/game_shared/bot/bot_manager.cpp +++ b/regamedll/game_shared/bot/bot_manager.cpp @@ -213,7 +213,7 @@ void CBotManager::StartFrame() const char *CBotManager::GetNavMapFilename() const { static char filename[256]; - Q_sprintf(filename, "maps\\%s.nav", STRING(gpGlobals->mapname)); + Q_snprintf(filename, sizeof(filename), "maps\\%s.nav", STRING(gpGlobals->mapname)); return filename; } diff --git a/regamedll/game_shared/bot/bot_profile.cpp b/regamedll/game_shared/bot/bot_profile.cpp index c025f903..76cb37a1 100644 --- a/regamedll/game_shared/bot/bot_profile.cpp +++ b/regamedll/game_shared/bot/bot_profile.cpp @@ -178,9 +178,10 @@ void BotProfileManager::Init(const char *filename, unsigned int *checksum) m_skins[m_nextSkin] = CloneString(decoratedName); // construct the model filename + int SkinLen = Q_strlen(token) * 2 + Q_strlen("models/player//.mdl"); m_skinModelnames[m_nextSkin] = CloneString(token); - m_skinFilenames[m_nextSkin] = new char[Q_strlen(token) * 2 + Q_strlen("models/player//.mdl") + 1]; - Q_sprintf(m_skinFilenames[m_nextSkin], "models/player/%s/%s.mdl", token, token); + m_skinFilenames[m_nextSkin] = new char[SkinLen + 1]; + Q_snprintf(m_skinFilenames[m_nextSkin], SkinLen + 1, "models/player/%s/%s.mdl", token, token); m_nextSkin++; } @@ -304,7 +305,7 @@ void BotProfileManager::Init(const char *filename, unsigned int *checksum) // found attribute name - keep it char attributeName[64]; - Q_strcpy(attributeName, token); + Q_strlcpy(attributeName, token); // eat '=' dataFile = SharedParse(dataFile); diff --git a/regamedll/game_shared/bot/bot_util.cpp b/regamedll/game_shared/bot/bot_util.cpp index 449b7e37..4d694b31 100644 --- a/regamedll/game_shared/bot/bot_util.cpp +++ b/regamedll/game_shared/bot/bot_util.cpp @@ -560,7 +560,7 @@ void CONSOLE_ECHO(const char *pszMsg, ...) static char szStr[1024]; va_start(argptr, pszMsg); - vsprintf(szStr, pszMsg, argptr); + Q_vsnprintf(szStr, sizeof(szStr), pszMsg, argptr); va_end(argptr); SERVER_PRINT(szStr); @@ -572,7 +572,7 @@ void CONSOLE_ECHO_LOGGED(const char *pszMsg, ...) static char szStr[1024]; va_start(argptr, pszMsg); - vsprintf(szStr, pszMsg, argptr); + Q_vsnprintf(szStr, sizeof(szStr), pszMsg, argptr); va_end(argptr); SERVER_PRINT(szStr); diff --git a/regamedll/game_shared/bot/nav_area.cpp b/regamedll/game_shared/bot/nav_area.cpp index 8e4baf0d..6c596051 100644 --- a/regamedll/game_shared/bot/nav_area.cpp +++ b/regamedll/game_shared/bot/nav_area.cpp @@ -3819,9 +3819,9 @@ void EditNavAreas(NavEditCmdType cmd) name = TheNavAreaGrid.IDToName(area->GetPlace()); if (name) - Q_strcpy(locName, name); + Q_strlcpy(locName, name); else - Q_strcpy(locName, "ERROR"); + Q_strlcpy(locName, "ERROR"); } else { diff --git a/regamedll/game_shared/bot/nav_file.cpp b/regamedll/game_shared/bot/nav_file.cpp index c9f206fd..5c6d65c3 100644 --- a/regamedll/game_shared/bot/nav_file.cpp +++ b/regamedll/game_shared/bot/nav_file.cpp @@ -632,12 +632,15 @@ bool SaveNavigationMap(const char *filename) void LoadLocationFile(const char *filename) { char locFilename[256]; - Q_strcpy(locFilename, filename); + Q_strlcpy(locFilename, filename); - char *dot = Q_strchr(locFilename, '.'); + char *dot = Q_strrchr(locFilename, '.'); if (dot) { - Q_strcpy(dot, ".loc"); + int dotlen = dot - locFilename; + size_t remaining_size = sizeof(locFilename) - dotlen; + if (remaining_size > 0) + Q_snprintf(dot, remaining_size, ".loc"); int locDataLength; char *locDataFile = (char *)LOAD_FILE_FOR_ME(const_cast(locFilename), &locDataLength); @@ -771,7 +774,7 @@ NavErrorType LoadNavigationMap() // nav filename is derived from map filename char filename[256]; - Q_sprintf(filename, "maps\\%s.nav", STRING(gpGlobals->mapname)); + Q_snprintf(filename, sizeof(filename), "maps\\%s.nav", STRING(gpGlobals->mapname)); // free previous navigation map data DestroyNavigationMap(); diff --git a/regamedll/pm_shared/pm_shared.cpp b/regamedll/pm_shared/pm_shared.cpp index fa106c09..e9efb376 100644 --- a/regamedll/pm_shared/pm_shared.cpp +++ b/regamedll/pm_shared/pm_shared.cpp @@ -123,7 +123,7 @@ void PM_InitTextureTypes() j = Q_min(j, MAX_TEXTURENAME_LENGHT - 1 + i); buffer[j] = '\0'; - Q_strcpy(&(pm_grgszTextureName[pm_gcTextures++][0]), &(buffer[i])); + Q_strlcpy(pm_grgszTextureName[pm_gcTextures++], &(buffer[i])); } // Must use engine to free since we are in a .dll @@ -364,8 +364,7 @@ void PM_CatagorizeTextureType() if (*pTextureName == '{' || *pTextureName == '!' || *pTextureName == '~' || *pTextureName == ' ') pTextureName++; - Q_strcpy(pmove->sztexturename, pTextureName); - pmove->sztexturename[MAX_TEXTURENAME_LENGHT - 1] = '\0'; + Q_strlcpy(pmove->sztexturename, pTextureName, MAX_TEXTURENAME_LENGHT); // get texture type pmove->chtexturetype = PM_FindTextureType(pmove->sztexturename); diff --git a/regamedll/public/strtools.h b/regamedll/public/strtools.h index 5cdf8c41..11279c76 100644 --- a/regamedll/public/strtools.h +++ b/regamedll/public/strtools.h @@ -156,40 +156,48 @@ inline char *Q_strlcpy(char *dest, const char *src, size_t size) { // a safe variant of strcpy that truncates the result to fit in the destination buffer template char *Q_strlcpy(char (&dest)[size], const char *src) { - return Q_strlcpy(dest, src, size); + return Q_strlcpy(static_cast(dest), src, size); } // safely concatenate two strings. // a variant of strcat that truncates the result to fit in the destination buffer -template -size_t Q_strlcat(char (&dest)[size], const char *src) +inline size_t Q_strlcat(char *dest, const char *src, size_t maxDestSize) { size_t srclen; // Length of source string size_t dstlen; // Length of destination string // Figure out how much room is left - dstlen = Q_strlen(dest); - size_t length = size - dstlen + 1; + dstlen = strlen(dest); + size_t unRemainingSize = maxDestSize - dstlen - 1; - if (!length) { - // No room, return immediately - return dstlen; + // Sanity check in case dest doesn't contain a null termination + if (dstlen > (maxDestSize - 1)) + dstlen = maxDestSize - 1; + + if (unRemainingSize <= 0 || unRemainingSize > maxDestSize) + { + dest[dstlen] = '\0'; + return dstlen; // No room, return immediately } // Figure out how much room is needed - srclen = Q_strlen(src); + srclen = strlen(src); // Copy the appropriate amount - if (srclen > length) { - srclen = length; - } + if (srclen > unRemainingSize) + srclen = unRemainingSize; Q_memcpy(dest + dstlen, src, srclen); dest[dstlen + srclen] = '\0'; - return dstlen + srclen; } +template +inline size_t Q_strlcat(char (&dest)[size], const char *src) +{ + return Q_strlcat(static_cast(dest), src, size); +} + // Force slashes of either type to be = separator character inline void Q_FixSlashes(char *pname, char separator = CORRECT_PATH_SEPARATOR) { diff --git a/regamedll/unittests/mathfun_tests.cpp b/regamedll/unittests/mathfun_tests.cpp index 6cb36eca..7d39cb3b 100644 --- a/regamedll/unittests/mathfun_tests.cpp +++ b/regamedll/unittests/mathfun_tests.cpp @@ -32,10 +32,10 @@ TEST(SinCosPrecision, SseMathFun, 10000) double sse_sin = _mm_cvtss_f32(s); double sse_cos = _mm_cvtss_f32(c); - sprintf(localbuf, "sin precision failure for angle=%f", i); + Q_snprintf(localbuf, sizeof(localbuf), "sin precision failure for angle=%f", i); DOUBLES_EQUAL(localbuf, x87_sin, sse_sin, 0.000001); - sprintf(localbuf, "cos precision failure for angle=%f", i); + Q_snprintf(localbuf, sizeof(localbuf), "cos precision failure for angle=%f", i); DOUBLES_EQUAL(localbuf, x87_cos, sse_cos, 0.000001); } }