Skip to content

Commit e72ebd4

Browse files
authored
build: Add static_assert's near relevant strcpy calls to prevent buffer overflows (TheSuperHackers#1806)
1 parent fcd0085 commit e72ebd4

File tree

8 files changed

+38
-0
lines changed

8 files changed

+38
-0
lines changed

Generals/Code/GameEngine/Source/GameNetwork/GameSpy/Thread/BuddyThread.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ void BuddyThreadClass::errorCallback( GPConnection *con, GPErrorArg *arg )
504504
static void getNickForMessage( GPConnection *con, GPGetInfoResponseArg *arg, void *param )
505505
{
506506
BuddyResponse *resp = (BuddyResponse *)param;
507+
static_assert(ARRAY_SIZE(resp->arg.message.nick) >= ARRAY_SIZE(arg->nick), "Incorrect array size");
507508
strcpy(resp->arg.message.nick, arg->nick);
508509
}
509510

@@ -618,6 +619,9 @@ static void getInfoResponseForRequest( GPConnection *con, GPGetInfoResponseArg *
618619
{
619620
BuddyResponse *resp = (BuddyResponse *)param;
620621
resp->profile = arg->profile;
622+
static_assert(ARRAY_SIZE(resp->arg.request.nick) >= ARRAY_SIZE(arg->nick), "Incorrect array size");
623+
static_assert(ARRAY_SIZE(resp->arg.request.email) >= ARRAY_SIZE(arg->email), "Incorrect array size");
624+
static_assert(ARRAY_SIZE(resp->arg.request.countrycode) >= ARRAY_SIZE(arg->countrycode), "Incorrect array size");
621625
strcpy(resp->arg.request.nick, arg->nick);
622626
strcpy(resp->arg.request.email, arg->email);
623627
strcpy(resp->arg.request.countrycode, arg->countrycode);
@@ -644,6 +648,9 @@ static void getInfoResponseForStatus(GPConnection * connection, GPGetInfoRespons
644648
{
645649
BuddyResponse *resp = (BuddyResponse *)param;
646650
resp->profile = arg->profile;
651+
static_assert(ARRAY_SIZE(resp->arg.status.nick) >= ARRAY_SIZE(arg->nick), "Incorrect array size");
652+
static_assert(ARRAY_SIZE(resp->arg.status.email) >= ARRAY_SIZE(arg->email), "Incorrect array size");
653+
static_assert(ARRAY_SIZE(resp->arg.status.countrycode) >= ARRAY_SIZE(arg->countrycode), "Incorrect array size");
647654
strcpy(resp->arg.status.nick, arg->nick);
648655
strcpy(resp->arg.status.email, arg->email);
649656
strcpy(resp->arg.status.countrycode, arg->countrycode);
@@ -660,6 +667,8 @@ void BuddyThreadClass::statusCallback( GPConnection *con, GPRecvBuddyStatusArg *
660667
// get user's status
661668
GPBuddyStatus status;
662669
gpGetBuddyStatus( con, arg->index, &status );
670+
static_assert(ARRAY_SIZE(response.arg.status.location) >= ARRAY_SIZE(status.locationString), "Incorrect array size");
671+
static_assert(ARRAY_SIZE(response.arg.status.statusString) >= ARRAY_SIZE(status.statusString), "Incorrect array size");
663672
strcpy(response.arg.status.location, status.locationString);
664673
strcpy(response.arg.status.statusString, status.statusString);
665674
response.arg.status.status = status.status;

Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,7 @@ W3DProjectedShadow* W3DProjectedShadowManager::addShadow(RenderObjClass *robj, S
17181718
//onto world geometry.
17191719
if (strlen(shadowInfo->m_ShadowName) <= 1) //no texture name given, use same as object
17201720
{
1721+
static_assert(ARRAY_SIZE(texture_name) >= ARRAY_SIZE(defaultDecalName), "Incorrect array size");
17211722
strcpy(texture_name, defaultDecalName);
17221723
}
17231724
else
@@ -1894,6 +1895,7 @@ W3DProjectedShadow* W3DProjectedShadowManager::createDecalShadow(Shadow::ShadowT
18941895
//onto world geometry.
18951896
if (strlen(shadowInfo->m_ShadowName) <= 1) //no texture name given, use same as object
18961897
{
1898+
static_assert(ARRAY_SIZE(texture_name) >= ARRAY_SIZE(defaultDecalName), "Incorrect array size");
18971899
strcpy(texture_name,defaultDecalName);
18981900
}
18991901
else

Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DFileSystem.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,15 @@ char const * GameFileClass::Set_Name( char const *filename )
157157
if( fileType == FILE_TYPE_W3D )
158158
{
159159

160+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(W3D_DIR_PATH), "Incorrect array size");
160161
strcpy( m_filePath, W3D_DIR_PATH );
161162
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
162163

163164
}
164165
else if( isImageFileType(fileType) )
165166
{
166167

168+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(TGA_DIR_PATH), "Incorrect array size");
167169
strcpy( m_filePath, TGA_DIR_PATH );
168170
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
169171

@@ -182,13 +184,15 @@ char const * GameFileClass::Set_Name( char const *filename )
182184
if( fileType == FILE_TYPE_W3D )
183185
{
184186

187+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(LEGACY_W3D_DIR_PATH), "Incorrect array size");
185188
strcpy( m_filePath, LEGACY_W3D_DIR_PATH );
186189
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
187190

188191
}
189192
else if( isImageFileType(fileType) )
190193
{
191194

195+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(LEGACY_TGA_DIR_PATH), "Incorrect array size");
192196
strcpy( m_filePath, LEGACY_TGA_DIR_PATH );
193197
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
194198

@@ -208,13 +212,15 @@ char const * GameFileClass::Set_Name( char const *filename )
208212
if( fileType == FILE_TYPE_W3D )
209213
{
210214

215+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(TEST_W3D_DIR_PATH), "Incorrect array size");
211216
strcpy( m_filePath, TEST_W3D_DIR_PATH );
212217
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
213218

214219
}
215220
else if( isImageFileType(fileType) )
216221
{
217222

223+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(TEST_TGA_DIR_PATH), "Incorrect array size");
218224
strcpy( m_filePath, TEST_TGA_DIR_PATH );
219225
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
220226

Generals/Code/Tools/WorldBuilder/src/RoadOptions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ BOOL RoadOptions::OnInitDialog()
246246
char findBuf[_MAX_PATH];
247247
char fileBuf[_MAX_PATH];
248248

249+
static_assert(ARRAY_SIZE(dirBuf) >= ARRAY_SIZE(ROAD_DIRECTORY), "Incorrect array size");
249250
strcpy(dirBuf, ROAD_DIRECTORY);
250251
int len = strlen(dirBuf);
251252

@@ -273,6 +274,7 @@ BOOL RoadOptions::OnInitDialog()
273274
++it;
274275
continue;
275276
}
277+
static_assert(ARRAY_SIZE(fileBuf) >= ARRAY_SIZE(TEST_STRING), "Incorrect array size");
276278
strcpy(fileBuf, TEST_STRING);
277279
strlcat(fileBuf, "\\", ARRAY_SIZE(fileBuf));
278280
strlcat(fileBuf, filename.str(), ARRAY_SIZE(fileBuf));

GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/Thread/BuddyThread.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ void BuddyThreadClass::errorCallback( GPConnection *con, GPErrorArg *arg )
504504
static void getNickForMessage( GPConnection *con, GPGetInfoResponseArg *arg, void *param )
505505
{
506506
BuddyResponse *resp = (BuddyResponse *)param;
507+
static_assert(ARRAY_SIZE(resp->arg.message.nick) >= ARRAY_SIZE(arg->nick), "Incorrect array size");
507508
strcpy(resp->arg.message.nick, arg->nick);
508509
}
509510

@@ -618,6 +619,9 @@ static void getInfoResponseForRequest( GPConnection *con, GPGetInfoResponseArg *
618619
{
619620
BuddyResponse *resp = (BuddyResponse *)param;
620621
resp->profile = arg->profile;
622+
static_assert(ARRAY_SIZE(resp->arg.request.nick) >= ARRAY_SIZE(arg->nick), "Incorrect array size");
623+
static_assert(ARRAY_SIZE(resp->arg.request.email) >= ARRAY_SIZE(arg->email), "Incorrect array size");
624+
static_assert(ARRAY_SIZE(resp->arg.request.countrycode) >= ARRAY_SIZE(arg->countrycode), "Incorrect array size");
621625
strcpy(resp->arg.request.nick, arg->nick);
622626
strcpy(resp->arg.request.email, arg->email);
623627
strcpy(resp->arg.request.countrycode, arg->countrycode);
@@ -644,6 +648,9 @@ static void getInfoResponseForStatus(GPConnection * connection, GPGetInfoRespons
644648
{
645649
BuddyResponse *resp = (BuddyResponse *)param;
646650
resp->profile = arg->profile;
651+
static_assert(ARRAY_SIZE(resp->arg.status.nick) >= ARRAY_SIZE(arg->nick), "Incorrect array size");
652+
static_assert(ARRAY_SIZE(resp->arg.status.email) >= ARRAY_SIZE(arg->email), "Incorrect array size");
653+
static_assert(ARRAY_SIZE(resp->arg.status.countrycode) >= ARRAY_SIZE(arg->countrycode), "Incorrect array size");
647654
strcpy(resp->arg.status.nick, arg->nick);
648655
strcpy(resp->arg.status.email, arg->email);
649656
strcpy(resp->arg.status.countrycode, arg->countrycode);
@@ -660,6 +667,8 @@ void BuddyThreadClass::statusCallback( GPConnection *con, GPRecvBuddyStatusArg *
660667
// get user's status
661668
GPBuddyStatus status;
662669
gpGetBuddyStatus( con, arg->index, &status );
670+
static_assert(ARRAY_SIZE(response.arg.status.location) >= ARRAY_SIZE(status.locationString), "Incorrect array size");
671+
static_assert(ARRAY_SIZE(response.arg.status.statusString) >= ARRAY_SIZE(status.statusString), "Incorrect array size");
663672
strcpy(response.arg.status.location, status.locationString);
664673
strcpy(response.arg.status.statusString, status.statusString);
665674
response.arg.status.status = status.status;

GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,7 @@ W3DProjectedShadow* W3DProjectedShadowManager::addShadow(RenderObjClass *robj, S
17181718
//onto world geometry.
17191719
if (strlen(shadowInfo->m_ShadowName) <= 1) //no texture name given, use same as object
17201720
{
1721+
static_assert(ARRAY_SIZE(texture_name) >= ARRAY_SIZE(defaultDecalName), "Incorrect array size");
17211722
strcpy(texture_name, defaultDecalName);
17221723
}
17231724
else
@@ -1894,6 +1895,7 @@ W3DProjectedShadow* W3DProjectedShadowManager::createDecalShadow(Shadow::ShadowT
18941895
//onto world geometry.
18951896
if (strlen(shadowInfo->m_ShadowName) <= 1) //no texture name given, use same as object
18961897
{
1898+
static_assert(ARRAY_SIZE(texture_name) >= ARRAY_SIZE(defaultDecalName), "Incorrect array size");
18971899
strcpy(texture_name, defaultDecalName);
18981900
}
18991901
else

GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DFileSystem.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,15 @@ char const * GameFileClass::Set_Name( char const *filename )
197197
if( fileType == FILE_TYPE_W3D )
198198
{
199199

200+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(W3D_DIR_PATH), "Incorrect array size");
200201
strcpy( m_filePath, W3D_DIR_PATH );
201202
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
202203

203204
}
204205
else if( isImageFileType(fileType) )
205206
{
206207

208+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(TGA_DIR_PATH), "Incorrect array size");
207209
strcpy( m_filePath, TGA_DIR_PATH );
208210
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
209211

@@ -225,13 +227,15 @@ char const * GameFileClass::Set_Name( char const *filename )
225227
if( fileType == FILE_TYPE_W3D )
226228
{
227229

230+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(LEGACY_W3D_DIR_PATH), "Incorrect array size");
228231
strcpy( m_filePath, LEGACY_W3D_DIR_PATH );
229232
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
230233

231234
}
232235
else if( isImageFileType(fileType) )
233236
{
234237

238+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(LEGACY_TGA_DIR_PATH), "Incorrect array size");
235239
strcpy( m_filePath, LEGACY_TGA_DIR_PATH );
236240
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
237241

@@ -253,13 +257,15 @@ char const * GameFileClass::Set_Name( char const *filename )
253257
if( fileType == FILE_TYPE_W3D )
254258
{
255259

260+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(TEST_W3D_DIR_PATH), "Incorrect array size");
256261
strcpy( m_filePath, TEST_W3D_DIR_PATH );
257262
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
258263

259264
}
260265
else if( isImageFileType(fileType) )
261266
{
262267

268+
static_assert(ARRAY_SIZE(m_filePath) >= ARRAY_SIZE(TEST_TGA_DIR_PATH), "Incorrect array size");
263269
strcpy( m_filePath, TEST_TGA_DIR_PATH );
264270
strlcat(m_filePath, filename, ARRAY_SIZE(m_filePath));
265271

GeneralsMD/Code/Tools/WorldBuilder/src/RoadOptions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ BOOL RoadOptions::OnInitDialog()
246246
char findBuf[_MAX_PATH];
247247
char fileBuf[_MAX_PATH];
248248

249+
static_assert(ARRAY_SIZE(dirBuf) >= ARRAY_SIZE(ROAD_DIRECTORY), "Incorrect array size");
249250
strcpy(dirBuf, ROAD_DIRECTORY);
250251
int len = strlen(dirBuf);
251252

@@ -273,6 +274,7 @@ BOOL RoadOptions::OnInitDialog()
273274
++it;
274275
continue;
275276
}
277+
static_assert(ARRAY_SIZE(fileBuf) >= ARRAY_SIZE(TEST_STRING), "Incorrect array size");
276278
strcpy(fileBuf, TEST_STRING);
277279
strlcat(fileBuf, "\\", ARRAY_SIZE(fileBuf));
278280
strlcat(fileBuf, filename.str(), ARRAY_SIZE(fileBuf));

0 commit comments

Comments
 (0)