diff --git a/include/corelib.h b/include/corelib.h index f84dbcb..994ef16 100644 --- a/include/corelib.h +++ b/include/corelib.h @@ -4075,3 +4075,8 @@ public: #define FOR_EACH_AE(arrayName, iteratorName) \ for (int iteratorName = 0; iteratorName != arrayName.GetElementNumber (); iteratorName++) + +// +// Sizeof bounds +// +#define SIZEOF_CHAR(in) sizeof (in) - 1 diff --git a/source/basecode.cpp b/source/basecode.cpp index 0ecc8e3..c40cde5 100644 --- a/source/basecode.cpp +++ b/source/basecode.cpp @@ -5123,14 +5123,14 @@ void Bot::BotAI (void) break; } - char enemyName[80], weaponName[80], aimFlags[32], botType[32]; + char enemyName[80], weaponName[80], aimFlags[64], botType[32]; if (!IsEntityNull (m_enemy)) - strcpy (enemyName, STRING (m_enemy->v.netname)); + strncpy (enemyName, STRING (m_enemy->v.netname), SIZEOF_CHAR (enemyName)); else if (!IsEntityNull (m_lastEnemy)) { strcpy (enemyName, " (L)"); - strcat (enemyName, STRING (m_lastEnemy->v.netname)); + strncat (enemyName, STRING (m_lastEnemy->v.netname), SIZEOF_CHAR (enemyName)); } else strcpy (enemyName, " (null)"); @@ -5139,7 +5139,7 @@ void Bot::BotAI (void) memset (pickupName, 0, sizeof (pickupName)); if (!IsEntityNull (m_pickupItem)) - strcpy (pickupName, STRING (m_pickupItem->v.classname)); + strncpy (pickupName, STRING (m_pickupItem->v.classname), SIZEOF_CHAR (pickupName)); else strcpy (pickupName, " (null)"); @@ -5195,7 +5195,7 @@ void Bot::BotAI (void) } } else - strncpy (weaponName, selectTab->weaponName, sizeof (weaponName)); + strncpy (weaponName, selectTab->weaponName, SIZEOF_CHAR (weaponName)); char outputBuffer[512]; memset (outputBuffer, 0, sizeof (outputBuffer)); diff --git a/source/chatlib.cpp b/source/chatlib.cpp index 55c009e..bd7931f 100644 --- a/source/chatlib.cpp +++ b/source/chatlib.cpp @@ -89,7 +89,7 @@ char *HumanizeName (char *name) // this function humanize player name (i.e. trim clan and switch to lower case (sometimes)) static char outputName[64]; // create return name buffer - strncpy (outputName, name, sizeof (outputName)); // copy name to new buffer + strncpy (outputName, name, SIZEOF_CHAR (outputName)); // copy name to new buffer // drop tag marks, 80 percent of time if (Random.Long (1, 100) < 80) @@ -156,6 +156,8 @@ void Bot::PrepareChatMessage (char *text) if (!yb_chat.GetBool () || IsNullString (text)) return; + #define ASSIGN_TALK_ENTITY() if (!IsEntityNull (talkEntity)) strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), SIZEOF_CHAR (m_tempStrings)) + memset (&m_tempStrings, 0, sizeof (m_tempStrings)); char *textStart = text; @@ -198,12 +200,11 @@ void Bot::PrepareChatMessage (char *text) } talkEntity = g_clients[index].ent; - if (!IsEntityNull (talkEntity)) - strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); + ASSIGN_TALK_ENTITY (); } // mapname? else if (*pattern == 'm') - strcat (m_tempStrings, GetMapName ()); + strncat (m_tempStrings, GetMapName (), SIZEOF_CHAR (m_tempStrings)); // roundtime? else if (*pattern == 'r') { @@ -214,9 +215,7 @@ void Bot::PrepareChatMessage (char *text) else if (*pattern == 's') { talkEntity = EntityOfIndex (m_sayTextBuffer.entityIndex); - - if (!IsEntityNull (talkEntity)) - strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); + ASSIGN_TALK_ENTITY (); } // teammate alive? else if (*pattern == 't') @@ -238,8 +237,7 @@ void Bot::PrepareChatMessage (char *text) else talkEntity = g_clients[i].ent; - if (!IsEntityNull (talkEntity)) - strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); + ASSIGN_TALK_ENTITY (); } else // no teammates alive... { @@ -255,8 +253,7 @@ void Bot::PrepareChatMessage (char *text) { talkEntity = g_clients[i].ent; - if (!IsEntityNull (talkEntity)) - strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); + ASSIGN_TALK_ENTITY (); } } } @@ -274,9 +271,7 @@ void Bot::PrepareChatMessage (char *text) if (i < GetMaxClients ()) { talkEntity = g_clients[i].ent; - - if (!IsEntityNull (talkEntity)) - strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); + ASSIGN_TALK_ENTITY (); } else // no teammates alive... { @@ -289,9 +284,7 @@ void Bot::PrepareChatMessage (char *text) if (i < GetMaxClients ()) { talkEntity = g_clients[i].ent; - - if (!IsEntityNull (talkEntity)) - strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); + ASSIGN_TALK_ENTITY (); } } } @@ -315,9 +308,7 @@ void Bot::PrepareChatMessage (char *text) else if (*pattern == 'v') { talkEntity = m_lastVictim; - - if (!IsEntityNull (talkEntity)) - strncat (m_tempStrings, HumanizeName (const_cast (STRING (talkEntity->v.netname))), sizeof (m_tempStrings)); + ASSIGN_TALK_ENTITY (); } pattern++; textStart = pattern; @@ -328,7 +319,7 @@ void Bot::PrepareChatMessage (char *text) { // let the bots make some mistakes... char tempString[160]; - strncpy (tempString, textStart, 159); + strncpy (tempString, textStart, SIZEOF_CHAR (tempString)); HumanizeChat (tempString); strcat (m_tempStrings, tempString); diff --git a/source/combat.cpp b/source/combat.cpp index afa9235..9283be8 100644 --- a/source/combat.cpp +++ b/source/combat.cpp @@ -53,7 +53,7 @@ bool Bot::IsEnemyHiddenByRendering (edict_t *enemy) entvars_t &v = enemy->v; - bool enemyHasGun = (v.weapons & WEAPON_SECONDARY) || (v.weapons & WEAPON_SECONDARY); + bool enemyHasGun = (v.weapons & WEAPON_PRIMARY) || (v.weapons & WEAPON_SECONDARY); bool enemyGunfire = (v.button & IN_ATTACK) || (v.oldbuttons & IN_ATTACK); if ((v.renderfx == kRenderFxExplode || (v.effects & EF_NODRAW)) && (!enemyGunfire || !enemyHasGun)) diff --git a/source/interface.cpp b/source/interface.cpp index 3d16d84..1450d50 100644 --- a/source/interface.cpp +++ b/source/interface.cpp @@ -488,7 +488,7 @@ void InitConfig (void) Array pair = String (line).Split ("\t\t"); if (pair.GetElementNumber () > 1) - strcpy (line, pair[0].Trim ().GetBuffer ()); + strncpy (line, pair[0].Trim ().GetBuffer (), SIZEOF_CHAR (line)); strtrim (line); line[32] = 0; @@ -516,7 +516,7 @@ void InitConfig (void) while (fp.GetBuffer (line, 255)) { SKIP_COMMENTS (); - strncpy (section, GetField (line, 0, 1), sizeof (section)); + strncpy (section, GetField (line, 0, 1), SIZEOF_CHAR (section)); if (strcmp (section, "[KILLED]") == 0) { @@ -1993,7 +1993,7 @@ void ClientCommand (edict_t *ent) if (IsNullString (CMD_ARGS ())) continue; - strncpy (target->m_sayTextBuffer.sayText, CMD_ARGS (), sizeof (target->m_sayTextBuffer.sayText)); + strncpy (target->m_sayTextBuffer.sayText, CMD_ARGS (), SIZEOF_CHAR (target->m_sayTextBuffer.sayText)); target->m_sayTextBuffer.timeNextChat = GetWorldTime () + target->m_sayTextBuffer.chatDelay; } } diff --git a/source/manager.cpp b/source/manager.cpp index 65323d6..0f3acae 100644 --- a/source/manager.cpp +++ b/source/manager.cpp @@ -131,7 +131,7 @@ int BotManager::CreateBot (const String &name, int difficulty, int personality, continue; pickedName->used = nameFound = true; - strncpy (outputName, pickedName->name, sizeof (outputName)); + strncpy (outputName, pickedName->name, SIZEOF_CHAR (outputName)); steamId = pickedName->steamId; } diff --git a/source/navigate.cpp b/source/navigate.cpp index f609156..5127b33 100644 --- a/source/navigate.cpp +++ b/source/navigate.cpp @@ -549,14 +549,7 @@ void Bot::CheckTerrain (float movedDistance, const Vector &dir, const Vector &di if (blockedRight) state[i] -= 5; } - else - { - state[i] = 0; - i++; - state[i] = 0; - } - - + // weighted all possible moves, now sort them to start with most probable bool isSorting = false; @@ -1254,6 +1247,7 @@ private: float pri; }; + int m_allocCount; int m_size; int m_heapSize; Node *m_heap; @@ -1269,7 +1263,9 @@ public: { m_size = 0; m_heapSize = initialSize; - m_heap = new Node[m_heapSize]; + m_allocCount = 0; + + m_heap = static_cast (malloc (sizeof (Node) * m_heapSize)); } inline ~PriorityQueue (void) @@ -1281,20 +1277,24 @@ public: // inserts a value into the priority queue inline void Push (int value, float pri) { + if (m_allocCount > 20) + { + AddLogEntry (false, LL_FATAL, "Tried to re-allocate heap too many times in pathfinder. This usually indicates corrupted waypoint file. Please obtain new copy of waypoint."); + return; + } + if (m_heap == NULL) return; if (m_size >= m_heapSize) { + m_allocCount++; m_heapSize += 100; Node *newHeap = static_cast (realloc (m_heap, sizeof (Node) * m_heapSize)); if (newHeap != NULL) - { m_heap = newHeap; - free (newHeap); - } } m_heap[m_size].pri = pri; diff --git a/source/netmsg.cpp b/source/netmsg.cpp index 4a7b020..869d82c 100644 --- a/source/netmsg.cpp +++ b/source/netmsg.cpp @@ -95,7 +95,7 @@ void NetworkMsg::Execute (void *p) switch (m_state) { case 0: - strncpy (weaponProp.className, PTR_TO_STR (p), sizeof (weaponProp.className)); + strncpy (weaponProp.className, PTR_TO_STR (p), SIZEOF_CHAR (weaponProp.className)); break; case 1: diff --git a/source/support.cpp b/source/support.cpp index f46a50c..b0c3a2b 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]; - strncpy (mapName, const_cast (g_pGlobals->pStringBase + static_cast (g_pGlobals->mapname)), sizeof (mapName)); + strncpy (mapName, const_cast (g_pGlobals->pStringBase + static_cast (g_pGlobals->mapname)), SIZEOF_CHAR (mapName)); return &mapName[0]; // and return a pointer to it } @@ -1222,7 +1222,7 @@ char *Localizer::TranslateInput (const char *input) if (ptr != input) ptr++; - strncpy (string, input, 1024); + strncpy (string, input, ARRAYSIZE_HLSDK (string)); strtrim (string); FOR_EACH_AE (m_langTab, i) diff --git a/source/waypoint.cpp b/source/waypoint.cpp index b444122..fceb7df 100644 --- a/source/waypoint.cpp +++ b/source/waypoint.cpp @@ -1075,13 +1075,20 @@ bool Waypoint::Load (void) if (fp.IsValid ()) { - fp.Read (&header, sizeof (header)); + if (fp.Read (&header, sizeof (WaypointHeader)) == 0) + { + sprintf (m_infoBuffer, "%s.pwf - damaged waypoint file (unable to read header)", GetMapName ()); + AddLogEntry (true, LL_ERROR, m_infoBuffer); + + fp.Close (); + return false; + } if (strncmp (header.header, FH_WAYPOINT, strlen (FH_WAYPOINT)) == 0) { if (header.fileVersion != FV_WAYPOINT) { - sprintf (m_infoBuffer, "%s.pwf - incorrect waypoint file version (expected '%d' found '%d')", GetMapName (), FV_WAYPOINT, static_cast (header.fileVersion)); + sprintf (m_infoBuffer, "%s.pwf - incorrect waypoint file version (expected '%d' found '%d')", GetMapName (), FV_WAYPOINT, header.fileVersion); AddLogEntry (true, LL_ERROR, m_infoBuffer); fp.Close (); @@ -1097,7 +1104,7 @@ bool Waypoint::Load (void) } else { - if (header.pointNumber == 0 || header.pointNumber >= MAX_WAYPOINTS) + 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); @@ -1221,8 +1228,8 @@ void Waypoint::Save (void) memset (header.header, 0, sizeof (header.header)); strcpy (header.header, FH_WAYPOINT); - strncpy (header.author, STRING (g_hostEntity->v.netname), sizeof (header.author)); - strncpy (header.mapName, GetMapName (), sizeof (header.mapName)); + strncpy (header.author, STRING (g_hostEntity->v.netname), SIZEOF_CHAR (header.author)); + strncpy (header.mapName, GetMapName (), SIZEOF_CHAR (header.mapName)); header.mapName[31] = 0; header.fileVersion = FV_WAYPOINT; @@ -2146,7 +2153,11 @@ bool Waypoint::LoadPathMatrix (void) int num = 0; // read number of waypoints - fp.Read (&num, sizeof (int)); + if (fp.Read (&num, sizeof (int)) == 0) + { + fp.Close (); + return false; + } if (num != g_numWaypoints) { @@ -2572,10 +2583,24 @@ WaypointDownloadError WaypointDownloader::DoDownload (void) timeout.tv_sec = 5; timeout.tv_usec = 0; - setsockopt (socketHandle, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof (timeout)); - setsockopt (socketHandle, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, sizeof (timeout)); + int result = 0; + result = setsockopt (socketHandle, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof (timeout)); + + if (result < 0) + { + FreeSocket (socketHandle); + return WDE_SOCKET_ERROR; + } + result = setsockopt (socketHandle, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof (timeout)); + + if (result < 0) + { + FreeSocket (socketHandle); + return WDE_SOCKET_ERROR; + } memset (&dest, 0, sizeof (dest)); + dest.sin_family = AF_INET; dest.sin_port = htons (80); dest.sin_addr.s_addr = inet_addr (inet_ntoa (*((struct in_addr *) host->h_addr)));