Skip to content

Commit c504d4d

Browse files
authored
[GEN][ZH] Prevent reading invalid data from 'nti', 'm_teams' in TeamsInfoRec::addTeam() (#1122)
1 parent 70d87f5 commit c504d4d

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

Generals/Code/GameEngine/Source/GameLogic/Map/SidesList.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ void TeamsInfoRec::clear()
10901090
{
10911091
Int i;
10921092

1093-
for (i = 0; i < m_numTeamsAllocated; i++)
1093+
for (i = 0; i < m_numTeams; ++i)
10941094
m_teams[i].clear();
10951095

10961096
m_numTeams = 0;
@@ -1101,7 +1101,7 @@ void TeamsInfoRec::clear()
11011101

11021102
TeamsInfo *TeamsInfoRec::findTeamInfo(AsciiString name, Int* index /*= NULL*/)
11031103
{
1104-
for (int i = 0; i < m_numTeams; i++)
1104+
for (int i = 0; i < m_numTeams; ++i)
11051105
{
11061106
if (m_teams[i].getDict()->getAsciiString(TheKey_teamName) == name)
11071107
{
@@ -1123,36 +1123,33 @@ void TeamsInfoRec::addTeam(const Dict* d)
11231123
DEBUG_ASSERTCRASH(m_numTeams < 1024, ("hmm, seems like an awful lot of teams..."));
11241124
if (m_numTeams >= m_numTeamsAllocated)
11251125
{
1126-
// pool[]ify
1127-
TeamsInfo* nti = NEW TeamsInfo[m_numTeamsAllocated + TEAM_ALLOC_CHUNK]; // throws on failure
1128-
1126+
// pool[]ify
1127+
const Int newNumTeamsAllocated = m_numTeams + TEAM_ALLOC_CHUNK;
1128+
TeamsInfo* nti = NEW TeamsInfo[newNumTeamsAllocated];
11291129
Int i;
11301130

1131-
for (i = 0; i < m_numTeams; i++)
1131+
for (i = 0; i < m_numTeams; ++i)
11321132
nti[i] = m_teams[i];
11331133

1134-
for ( ; i < m_numTeamsAllocated + TEAM_ALLOC_CHUNK; i++)
1135-
nti[i].clear();
1136-
11371134
delete [] m_teams;
1138-
11391135
m_teams = nti;
1140-
m_numTeamsAllocated += TEAM_ALLOC_CHUNK;
1136+
m_numTeamsAllocated = newNumTeamsAllocated;
11411137
}
11421138

1143-
m_teams[m_numTeams++].init(d);
1139+
m_teams[m_numTeams].init(d);
1140+
1141+
++m_numTeams;
11441142
}
11451143

11461144
void TeamsInfoRec::removeTeam(Int i)
11471145
{
11481146
if (i < 0 || i >= m_numTeams || m_numTeams <= 1)
11491147
return;
11501148

1151-
for ( ; i < m_numTeams-1; i++)
1152-
m_teams[i] = m_teams[i+1];
1149+
--m_numTeams;
11531150

1154-
for ( ; i < m_numTeamsAllocated; i++)
1155-
m_teams[i].clear();
1151+
for ( ; i < m_numTeams; ++i)
1152+
m_teams[i] = m_teams[i+1];
11561153

1157-
--m_numTeams;
1154+
m_teams[m_numTeams].clear();
11581155
}

GeneralsMD/Code/GameEngine/Source/GameLogic/Map/SidesList.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ void TeamsInfoRec::clear()
11001100
{
11011101
Int i;
11021102

1103-
for (i = 0; i < m_numTeamsAllocated; i++)
1103+
for (i = 0; i < m_numTeams; ++i)
11041104
m_teams[i].clear();
11051105

11061106
m_numTeams = 0;
@@ -1111,7 +1111,7 @@ void TeamsInfoRec::clear()
11111111

11121112
TeamsInfo *TeamsInfoRec::findTeamInfo(AsciiString name, Int* index /*= NULL*/)
11131113
{
1114-
for (int i = 0; i < m_numTeams; i++)
1114+
for (int i = 0; i < m_numTeams; ++i)
11151115
{
11161116
if (m_teams[i].getDict()->getAsciiString(TheKey_teamName) == name)
11171117
{
@@ -1133,36 +1133,33 @@ void TeamsInfoRec::addTeam(const Dict* d)
11331133
DEBUG_ASSERTCRASH(m_numTeams < 2048, ("%d teams have been allocated (so far). This seems excessive.", m_numTeams ));
11341134
if (m_numTeams >= m_numTeamsAllocated)
11351135
{
1136-
// pool[]ify
1137-
TeamsInfo* nti = NEW TeamsInfo[m_numTeamsAllocated + TEAM_ALLOC_CHUNK]; // throws on failure
1138-
1136+
// pool[]ify
1137+
const Int newNumTeamsAllocated = m_numTeams + TEAM_ALLOC_CHUNK;
1138+
TeamsInfo* nti = NEW TeamsInfo[newNumTeamsAllocated];
11391139
Int i;
11401140

1141-
for (i = 0; i < m_numTeams; i++)
1141+
for (i = 0; i < m_numTeams; ++i)
11421142
nti[i] = m_teams[i];
11431143

1144-
for ( ; i < m_numTeamsAllocated + TEAM_ALLOC_CHUNK; i++)
1145-
nti[i].clear();
1146-
11471144
delete [] m_teams;
1148-
11491145
m_teams = nti;
1150-
m_numTeamsAllocated += TEAM_ALLOC_CHUNK;
1146+
m_numTeamsAllocated = newNumTeamsAllocated;
11511147
}
11521148

1153-
m_teams[m_numTeams++].init(d);
1149+
m_teams[m_numTeams].init(d);
1150+
1151+
++m_numTeams;
11541152
}
11551153

11561154
void TeamsInfoRec::removeTeam(Int i)
11571155
{
11581156
if (i < 0 || i >= m_numTeams || m_numTeams <= 1)
11591157
return;
11601158

1161-
for ( ; i < m_numTeams-1; i++)
1162-
m_teams[i] = m_teams[i+1];
1159+
--m_numTeams;
11631160

1164-
for ( ; i < m_numTeamsAllocated; i++)
1165-
m_teams[i].clear();
1161+
for ( ; i < m_numTeams; ++i)
1162+
m_teams[i] = m_teams[i+1];
11661163

1167-
--m_numTeams;
1164+
m_teams[m_numTeams].clear();
11681165
}

0 commit comments

Comments
 (0)