From 95c121ef4307a79634c4eed8989d7c94a0f7b111 Mon Sep 17 00:00:00 2001 From: jeefo Date: Mon, 29 Jun 2015 20:51:25 +0300 Subject: [PATCH] fixed some coverity warnings --- include/compress.h | 6 ++++ include/core.h | 22 ++++++++++---- source/basecode.cpp | 15 ++++++++-- source/chatlib.cpp | 18 ++++++------ source/interface.cpp | 15 ++++++---- source/manager.cpp | 8 ++--- source/navigate.cpp | 23 +++++++++------ source/netmsg.cpp | 2 +- source/support.cpp | 5 ++-- source/waypoint.cpp | 69 ++++++++++++++++++++++++++++++++++---------- 10 files changed, 127 insertions(+), 56 deletions(-) diff --git a/include/compress.h b/include/compress.h index 200ac5a..9151f77 100644 --- a/include/compress.h +++ b/include/compress.h @@ -148,12 +148,18 @@ public: { m_textSize = 0; m_codeSize = 0; + + m_matchPosition = 0; + m_matchLength = 0; } ~Compressor (void) { m_textSize = 0; m_codeSize = 0; + + m_matchPosition = 0; + m_matchLength = 0; } int InternalEncode (const char *fileName, byte *header, int headerSize, byte *buffer, int bufferSize) diff --git a/include/core.h b/include/core.h index e189106..eb99f9b 100644 --- a/include/core.h +++ b/include/core.h @@ -110,14 +110,12 @@ public: Library (const char *fileName) { + m_ptr = NULL; + if (fileName == NULL) return; -#ifdef PLATFORM_WIN32 - m_ptr = LoadLibrary (fileName); -#else - m_ptr = dlopen (fileName, RTLD_NOW); -#endif + LoadLib (fileName); } ~Library (void) @@ -133,6 +131,16 @@ public: } public: + inline void *LoadLib (const char *fileName) + { +#ifdef PLATFORM_WIN32 + m_ptr = LoadLibrary (fileName); +#else + m_ptr = dlopen (fileName, RTLD_NOW); +#endif + + return m_ptr; + } void *GetFunctionAddr (const char *functionName) { if (!IsLoaded ()) @@ -1050,7 +1058,7 @@ private: void RunPlayerMovement (void); byte ThrottledMsec (void); void GetValidWaypoint (void); - void ChangeWptIndex (int waypointIndex); + int ChangeWptIndex (int waypointIndex); bool IsDeadlyDrop (const Vector &to); bool OutOfBombTimer (void); void SelectLeaderEachTeam (int team); @@ -1580,6 +1588,8 @@ public: public: ConVar (const char *name, const char *initval, VarType type = VT_NORMAL) { + m_eptr = NULL; + g_convarWrapper->RegisterVariable (name, initval, type, this); } diff --git a/source/basecode.cpp b/source/basecode.cpp index ea37071..0ecc8e3 100644 --- a/source/basecode.cpp +++ b/source/basecode.cpp @@ -1406,7 +1406,6 @@ void Bot::CheckMessageQueue (void) TeamSayText (m_tempStrings); break; - case GSM_IDLE: default: return; } @@ -5196,7 +5195,7 @@ void Bot::BotAI (void) } } else - strcpy (weaponName, selectTab->weaponName); + strncpy (weaponName, selectTab->weaponName, sizeof (weaponName)); char outputBuffer[512]; memset (outputBuffer, 0, sizeof (outputBuffer)); @@ -5961,12 +5960,22 @@ void Bot::ReactOnSound (void) float maxVolume = 0.0, volume = 0.0; int hearEnemyIndex = -1; + Vector testHearing = EyePosition (); + + if (pev->flags & FL_DUCKING) + testHearing += VEC_HULL_MIN - VEC_DUCK_HULL_MIN; + + unsigned char *pas = ENGINE_SET_PAS ((reinterpret_cast (&testHearing))); + // loop through all enemy clients to check for hearable stuff for (int i = 0; i < GetMaxClients (); i++) { if (!(g_clients[i].flags & CF_USED) || !(g_clients[i].flags & CF_ALIVE) || g_clients[i].ent == GetEntity () || g_clients[i].timeSoundLasting < GetWorldTime ()) continue; + if (!ENGINE_CHECK_VISIBILITY (g_clients[i].ent, pas)) + continue; + float distance = (g_clients[i].soundPosition - pev->origin).GetLength (); float hearingDistance = g_clients[i].hearingDistance; @@ -5981,7 +5990,7 @@ void Bot::ReactOnSound (void) else volume = 2.0 * hearingDistance * (1.0 - distance / hearingDistance) * (g_clients[i].timeSoundLasting - GetWorldTime ()) / g_clients[i].maxTimeSoundLasting; - if (g_clients[hearEnemyIndex].team == m_team && yb_csdm_mode.GetInt () != 2) + if (g_clients[i].team == m_team && yb_csdm_mode.GetInt () != 2) volume = 0.3 * volume; // we will care about the most hearable sound instead of the closest one - KWo diff --git a/source/chatlib.cpp b/source/chatlib.cpp index 27dd2a7..55c009e 100644 --- a/source/chatlib.cpp +++ b/source/chatlib.cpp @@ -88,8 +88,8 @@ char *HumanizeName (char *name) { // this function humanize player name (i.e. trim clan and switch to lower case (sometimes)) - static char outputName[256]; // create return name buffer - strcpy (outputName, name); // copy name to new buffer + static char outputName[64]; // create return name buffer + strncpy (outputName, name, sizeof (outputName)); // copy name to new buffer // drop tag marks, 80 percent of time if (Random.Long (1, 100) < 80) @@ -199,7 +199,7 @@ void Bot::PrepareChatMessage (char *text) talkEntity = g_clients[index].ent; if (!IsEntityNull (talkEntity)) - strcat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname)))); + strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); } // mapname? else if (*pattern == 'm') @@ -216,7 +216,7 @@ void Bot::PrepareChatMessage (char *text) talkEntity = EntityOfIndex (m_sayTextBuffer.entityIndex); if (!IsEntityNull (talkEntity)) - strcat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname)))); + strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); } // teammate alive? else if (*pattern == 't') @@ -239,7 +239,7 @@ void Bot::PrepareChatMessage (char *text) talkEntity = g_clients[i].ent; if (!IsEntityNull (talkEntity)) - strcat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname)))); + strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); } else // no teammates alive... { @@ -256,7 +256,7 @@ void Bot::PrepareChatMessage (char *text) talkEntity = g_clients[i].ent; if (!IsEntityNull (talkEntity)) - strcat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname)))); + strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); } } } @@ -276,7 +276,7 @@ void Bot::PrepareChatMessage (char *text) talkEntity = g_clients[i].ent; if (!IsEntityNull (talkEntity)) - strcat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname)))); + strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); } else // no teammates alive... { @@ -291,7 +291,7 @@ void Bot::PrepareChatMessage (char *text) talkEntity = g_clients[i].ent; if (!IsEntityNull (talkEntity)) - strcat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname)))); + strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); } } } @@ -317,7 +317,7 @@ void Bot::PrepareChatMessage (char *text) talkEntity = m_lastVictim; if (!IsEntityNull (talkEntity)) - strcat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname)))); + strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); } pattern++; textStart = pattern; diff --git a/source/interface.cpp b/source/interface.cpp index ab6102e..3d16d84 100644 --- a/source/interface.cpp +++ b/source/interface.cpp @@ -516,7 +516,7 @@ void InitConfig (void) while (fp.GetBuffer (line, 255)) { SKIP_COMMENTS (); - strcpy (section, GetField (line, 0, 1)); + strncpy (section, GetField (line, 0, 1), sizeof (section)); if (strcmp (section, "[KILLED]") == 0) { @@ -1988,13 +1988,13 @@ void ClientCommand (edict_t *ent) if (target != NULL) { - target->m_sayTextBuffer.entityIndex = IndexOfEntity (ent); + target->m_sayTextBuffer.entityIndex = IndexOfEntity (ent); if (IsNullString (CMD_ARGS ())) continue; - strcpy (target->m_sayTextBuffer.sayText, CMD_ARGS ()); - target->m_sayTextBuffer.timeNextChat = GetWorldTime () + target->m_sayTextBuffer.chatDelay; + strncpy (target->m_sayTextBuffer.sayText, CMD_ARGS (), sizeof (target->m_sayTextBuffer.sayText)); + target->m_sayTextBuffer.timeNextChat = GetWorldTime () + target->m_sayTextBuffer.chatDelay; } } } @@ -3114,7 +3114,7 @@ DLL_GIVEFNPTRSTODLL GiveFnptrsToDll (enginefuncs_t *functionTable, globalvars_t ); g_gameLib = new Library (gameDLLName); - if (!g_gameLib->IsLoaded()) + if (!g_gameLib->IsLoaded ()) { // try to extract the game dll out of the steam cache AddLogEntry (true, LL_WARNING | LL_IGNORE, "Trying to extract dll '%s' out of the steam cache", gameDLLName); @@ -3135,7 +3135,10 @@ DLL_GIVEFNPTRSTODLL GiveFnptrsToDll (enginefuncs_t *functionTable, globalvars_t } FREE_FILE (buffer); } - g_gameLib = new Library (gameDLLName); + g_gameLib->LoadLib (gameDLLName); + + if (!g_gameLib->IsLoaded ()) + AddLogEntry (true, LL_FATAL | LL_IGNORE, "Unable to load gamedll \"%s\". Exiting... (gamedir: %s)", gameDLLName, GetModName ()); } } else diff --git a/source/manager.cpp b/source/manager.cpp index 8a37b82..65323d6 100644 --- a/source/manager.cpp +++ b/source/manager.cpp @@ -36,8 +36,8 @@ BotManager::BotManager (void) memset (m_bots, 0, sizeof (m_bots)); - if (g_pGlobals != NULL) - InitQuota (); + m_maintainTime = 0.0f; + m_creationTab.RemoveAll (); } BotManager::~BotManager (void) @@ -131,7 +131,7 @@ int BotManager::CreateBot (const String &name, int difficulty, int personality, continue; pickedName->used = nameFound = true; - strcpy (outputName, pickedName->name); + strncpy (outputName, pickedName->name, sizeof (outputName)); steamId = pickedName->steamId; } @@ -167,7 +167,7 @@ int BotManager::CreateBot (const String &name, int difficulty, int personality, m_bots[index] = new Bot (bot, difficulty, personality, team, member, steamId); - if (m_bots == NULL) + if (m_bots[index] == NULL) TerminateOnMalloc (); ServerPrint ("Connecting Bot..."); diff --git a/source/navigate.cpp b/source/navigate.cpp index e5289d6..f609156 100644 --- a/source/navigate.cpp +++ b/source/navigate.cpp @@ -224,7 +224,7 @@ TacticChoosen: } if (m_currentWaypointIndex == -1 || m_currentWaypointIndex >= g_numWaypoints) - ChangeWptIndex (g_waypoint->FindNearest (pev->origin)); + m_currentWaypointIndex = ChangeWptIndex (g_waypoint->FindNearest (pev->origin)); if (goalChoices[0] == -1) return m_chosenGoalIndex = Random.Long (0, g_numWaypoints - 1); @@ -410,11 +410,11 @@ void Bot::CheckTerrain (float movedDistance, const Vector &dir, const Vector &di int bits = 0; if (IsOnLadder ()) - bits = PROBE_STRAFE; + bits |= PROBE_STRAFE; else if (IsInWater ()) - bits = (PROBE_JUMP | PROBE_STRAFE); + bits |= (PROBE_JUMP | PROBE_STRAFE); else - bits = ((Random.Long (0, 10) > (cantMoveForward ? 7 : 5) ? PROBE_JUMP : 0) | PROBE_STRAFE | PROBE_DUCK); + bits |= ((Random.Long (0, 10) > (cantMoveForward ? 7 : 5) ? PROBE_JUMP : 0) | PROBE_STRAFE | PROBE_DUCK); // collision check allowed if not flying through the air if (IsOnFloor () || IsOnLadder () || IsInWater ()) @@ -553,7 +553,6 @@ void Bot::CheckTerrain (float movedDistance, const Vector &dir, const Vector &di { state[i] = 0; i++; - state[i] = 0; } @@ -1275,7 +1274,7 @@ public: inline ~PriorityQueue (void) { - delete[] m_heap; + free (m_heap); m_heap = NULL; } @@ -1662,6 +1661,7 @@ void Bot::FindPath (int srcIndex, int destIndex, unsigned char pathType) break; } + // put start node into open list astar[srcIndex].g = gcalc (srcIndex, -1); astar[srcIndex].f = astar[srcIndex].g + hcalc (srcIndex, srcIndex, destIndex); @@ -1753,7 +1753,7 @@ int Bot::GetAimingWaypoint (const Vector &to) // return the most distant waypoint which is seen from the Bot to the Target and is within count if (m_currentWaypointIndex == -1) - ChangeWptIndex (g_waypoint->FindNearest (pev->origin)); + m_currentWaypointIndex = ChangeWptIndex (g_waypoint->FindNearest (pev->origin)); int srcIndex = m_currentWaypointIndex; int destIndex = g_waypoint->FindNearest (to); @@ -1996,10 +1996,10 @@ void Bot::GetValidWaypoint (void) } } -void Bot::ChangeWptIndex (int waypointIndex) +int Bot::ChangeWptIndex(int waypointIndex) { if (waypointIndex == -1) - return; + return 0; m_prevWptIndex[4] = m_prevWptIndex[3]; m_prevWptIndex[3] = m_prevWptIndex[2]; @@ -2011,6 +2011,8 @@ void Bot::ChangeWptIndex (int waypointIndex) m_currentPath = g_waypoint->GetPath (m_currentWaypointIndex); m_waypointFlags = m_currentPath->flags; + + return m_currentWaypointIndex; // to satisfy static-code analyzers } int Bot::ChooseBombWaypoint (void) @@ -3066,6 +3068,9 @@ int Bot::GetAimingWaypoint (void) int currentWaypoint = g_waypoint->FindNearest (pev->origin); + if (currentWaypoint == -1) + return Random.Long (0, g_numWaypoints - 1); + for (int i = 0; i < g_numWaypoints; i++) { if (currentWaypoint == i || !g_waypoint->IsVisible (currentWaypoint, i)) diff --git a/source/netmsg.cpp b/source/netmsg.cpp index 102d21b..4a7b020 100644 --- a/source/netmsg.cpp +++ b/source/netmsg.cpp @@ -95,7 +95,7 @@ void NetworkMsg::Execute (void *p) switch (m_state) { case 0: - strcpy (weaponProp.className, PTR_TO_STR (p)); + strncpy (weaponProp.className, PTR_TO_STR (p), sizeof (weaponProp.className)); break; case 1: diff --git a/source/support.cpp b/source/support.cpp index f7cf465..f46a50c 100644 --- a/source/support.cpp +++ b/source/support.cpp @@ -941,7 +941,7 @@ const char *GetMapName (void) // this function gets the map name and store it in the map_name global string variable. static char mapName[256]; - strcpy (mapName, const_cast (g_pGlobals->pStringBase + static_cast (g_pGlobals->mapname))); + strncpy (mapName, const_cast (g_pGlobals->pStringBase + static_cast (g_pGlobals->mapname)), sizeof (mapName)); return &mapName[0]; // and return a pointer to it } @@ -1118,10 +1118,11 @@ float GetWaveLength (const char *fileName) return 0; } + // whoa, what a shit, is this working ?! char ch[32]; sprintf (ch, "0.%u", static_cast (waveHdr.dataChunkLength)); - float secondLength = static_cast (waveHdr.dataChunkLength / waveHdr.bytesPerSecond); + float secondLength = static_cast (waveHdr.dataChunkLength) / static_cast (waveHdr.bytesPerSecond); float milliSecondLength = atof (ch); return (secondLength == 0.0 ? milliSecondLength : secondLength); diff --git a/source/waypoint.cpp b/source/waypoint.cpp index e591a2a..b444122 100644 --- a/source/waypoint.cpp +++ b/source/waypoint.cpp @@ -875,7 +875,6 @@ void Waypoint::SaveExperienceTab (void) void Waypoint::InitExperienceTab (void) { - ExtensionHeader header; int i, j; delete [] g_experienceData; @@ -907,7 +906,16 @@ void Waypoint::InitExperienceTab (void) // if file exists, read the experience data from it if (fp.IsValid ()) { - fp.Read (&header, sizeof (ExtensionHeader)); + ExtensionHeader header; + memset (&header, 0, sizeof (header)); + + if (fp.Read (&header, sizeof (ExtensionHeader)) == 0) + { + AddLogEntry (true, LL_ERROR, "Experience data damaged (unable to read header)"); + + fp.Close (); + return; + } fp.Close (); if (strncmp (header.header, FH_EXPERIENCE, strlen (FH_EXPERIENCE)) == 0) @@ -956,10 +964,8 @@ void Waypoint::SaveVisibilityTab (void) if (g_numWaypoints == 0) return; - if (m_visLUT == NULL) - AddLogEntry (true, LL_FATAL, "Can't save visibility tab. Bad data."); - ExtensionHeader header; + memset (&header, 0, sizeof (ExtensionHeader)); // parse header memset (header.header, 0, sizeof (header.header)); @@ -1000,7 +1006,13 @@ void Waypoint::InitVisibilityTab (void) } // read the header of the file - fp.Read (&header, sizeof (ExtensionHeader)); + if (fp.Read (&header, sizeof (ExtensionHeader)) == 0) + { + AddLogEntry (true, LL_ERROR, "Vistable damaged (unable to read header)"); + + fp.Close (); + return; + } if (strncmp (header.header, FH_VISTABLE, strlen (FH_VISTABLE)) != 0 || header.fileVersion != FV_VISTABLE || header.pointNumber != g_numWaypoints) { @@ -1056,9 +1068,11 @@ void Waypoint::InitTypes (void) bool Waypoint::Load (void) { - WaypointHeader header; File fp (CheckSubfolderFile (), "rb"); + WaypointHeader header; + memset (&header, 0, sizeof (WaypointHeader)); + if (fp.IsValid ()) { fp.Read (&header, sizeof (header)); @@ -1067,7 +1081,7 @@ bool Waypoint::Load (void) { if (header.fileVersion != FV_WAYPOINT) { - sprintf (m_infoBuffer, "%s.pwf - incorrect waypoint file version (expected '%i' found '%i')", GetMapName (), FV_WAYPOINT, static_cast (header.fileVersion)); + sprintf (m_infoBuffer, "%s.pwf - incorrect waypoint file version (expected '%d' found '%d')", GetMapName (), FV_WAYPOINT, static_cast (header.fileVersion)); AddLogEntry (true, LL_ERROR, m_infoBuffer); fp.Close (); @@ -1083,6 +1097,15 @@ bool Waypoint::Load (void) } else { + if (header.pointNumber == 0 || header.pointNumber >= MAX_WAYPOINTS) + { + sprintf (m_infoBuffer, "%s.pwf - waypoint file contains illegal number of waypoints (mapname: '%s', header: '%s')", GetMapName (), GetMapName (), header.mapName); + AddLogEntry (true, LL_ERROR, m_infoBuffer); + + fp.Close (); + return false; + } + Init (); g_numWaypoints = header.pointNumber; @@ -1198,8 +1221,8 @@ void Waypoint::Save (void) memset (header.header, 0, sizeof (header.header)); strcpy (header.header, FH_WAYPOINT); - strcpy (header.author, STRING (g_hostEntity->v.netname)); - strncpy (header.mapName, GetMapName (), 31); + strncpy (header.author, STRING (g_hostEntity->v.netname), sizeof (header.author)); + strncpy (header.mapName, GetMapName (), sizeof (header.mapName)); header.mapName[31] = 0; header.fileVersion = FV_WAYPOINT; @@ -2134,11 +2157,18 @@ bool Waypoint::LoadPathMatrix (void) } // read path & distance matrixes - fp.Read (m_pathMatrix, sizeof (int), g_numWaypoints * g_numWaypoints); - fp.Read (m_distMatrix, sizeof (int), g_numWaypoints * g_numWaypoints); + if (fp.Read (m_pathMatrix, sizeof (int), g_numWaypoints * g_numWaypoints) == 0) + { + fp.Close (); + return false; + } - // and close the file - fp.Close (); + if (fp.Read (m_distMatrix, sizeof (int), g_numWaypoints * g_numWaypoints) == 0) + { + fp.Close (); + return false; + } + fp.Close (); // and close the file return true; } @@ -2467,6 +2497,7 @@ Waypoint::Waypoint (void) m_lastJumpWaypoint = -1; m_cacheWaypointIndex = -1; m_findWPIndex = -1; + m_facingAtIndex = -1; m_visibilityIndex = 0; m_isOnLadder = false; @@ -2483,6 +2514,9 @@ Waypoint::Waypoint (void) m_distMatrix = NULL; m_pathMatrix = NULL; + + for (int i = 0; i < MAX_WAYPOINTS; i++) + m_paths[i] = NULL; } Waypoint::~Waypoint (void) @@ -2614,8 +2648,11 @@ WaypointDownloadError WaypointDownloader::DoDownload (void) { recvSize = recv (socketHandle, buffer, ChunkSize, 0); - fp.Write (buffer, recvSize); - fp.Flush (); + if (recvSize > 0) + { + fp.Write (buffer, recvSize); + fp.Flush (); + } } while (recvSize != 0);