more fixes for coverity, now fixed what i have done in previous build :)

This commit is contained in:
jeefo 2015-06-29 21:49:52 +03:00
commit ff51914ea6
10 changed files with 75 additions and 54 deletions

View file

@ -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

View file

@ -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));

View file

@ -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 <char *> (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 <char *> (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 <char *> (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 <char *> (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 <char *> (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 <char *> (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 <char *> (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 <char *> (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);

View file

@ -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))

View file

@ -488,7 +488,7 @@ void InitConfig (void)
Array <String> 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;
}
}

View file

@ -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;
}

View file

@ -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 <Node *> (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 <Node *> (realloc (m_heap, sizeof (Node) * m_heapSize));
if (newHeap != NULL)
{
m_heap = newHeap;
free (newHeap);
}
}
m_heap[m_size].pri = pri;

View file

@ -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:

View file

@ -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 <const char *> (g_pGlobals->pStringBase + static_cast <int> (g_pGlobals->mapname)), sizeof (mapName));
strncpy (mapName, const_cast <const char *> (g_pGlobals->pStringBase + static_cast <int> (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)

View file

@ -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 <int> (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)));