Skip to content

Commit 038f51f

Browse files
authored
fix: Replace strcpy with strlcpy to prevent potential buffer overflows (#1808)
1 parent 07244b8 commit 038f51f

File tree

80 files changed

+259
-262
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+259
-262
lines changed

Core/GameEngine/Source/Common/System/Debug.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ void DebugInit(int flags)
381381
*(pEnd + 1) = 0;
382382
}
383383

384+
static_assert(ARRAY_SIZE(theLogFileNamePrev) >= ARRAY_SIZE(dirbuf), "Incorrect array size");
384385
strcpy(theLogFileNamePrev, dirbuf);
385386
strlcat(theLogFileNamePrev, gAppPrefix, ARRAY_SIZE(theLogFileNamePrev));
386387
strlcat(theLogFileNamePrev, DEBUG_FILE_NAME_PREV, ARRAY_SIZE(theLogFileNamePrev));
@@ -391,6 +392,7 @@ void DebugInit(int flags)
391392
}
392393
strlcat(theLogFileNamePrev, ".txt", ARRAY_SIZE(theLogFileNamePrev));
393394

395+
static_assert(ARRAY_SIZE(theLogFileName) >= ARRAY_SIZE(dirbuf), "Incorrect array size");
394396
strcpy(theLogFileName, dirbuf);
395397
strlcat(theLogFileName, gAppPrefix, ARRAY_SIZE(theLogFileNamePrev));
396398
strlcat(theLogFileName, DEBUG_FILE_NAME, ARRAY_SIZE(theLogFileNamePrev));
@@ -730,9 +732,9 @@ void ReleaseCrash(const char *reason)
730732
return; // We are shutting down, and TheGlobalData has been freed. jba. [4/15/2003]
731733
}
732734

733-
strcpy(prevbuf, TheGlobalData->getPath_UserData().str());
735+
strlcpy(prevbuf, TheGlobalData->getPath_UserData().str(), ARRAY_SIZE(prevbuf));
734736
strlcat(prevbuf, RELEASECRASH_FILE_NAME_PREV, ARRAY_SIZE(prevbuf));
735-
strcpy(curbuf, TheGlobalData->getPath_UserData().str());
737+
strlcpy(curbuf, TheGlobalData->getPath_UserData().str(), ARRAY_SIZE(curbuf));
736738
strlcat(curbuf, RELEASECRASH_FILE_NAME, ARRAY_SIZE(curbuf));
737739

738740
remove(prevbuf);
@@ -819,9 +821,9 @@ void ReleaseCrashLocalized(const AsciiString& p, const AsciiString& m)
819821
char prevbuf[ _MAX_PATH ];
820822
char curbuf[ _MAX_PATH ];
821823

822-
strcpy(prevbuf, TheGlobalData->getPath_UserData().str());
824+
strlcpy(prevbuf, TheGlobalData->getPath_UserData().str(), ARRAY_SIZE(prevbuf));
823825
strlcat(prevbuf, RELEASECRASH_FILE_NAME_PREV, ARRAY_SIZE(prevbuf));
824-
strcpy(curbuf, TheGlobalData->getPath_UserData().str());
826+
strlcpy(curbuf, TheGlobalData->getPath_UserData().str(), ARRAY_SIZE(curbuf));
825827
strlcat(curbuf, RELEASECRASH_FILE_NAME, ARRAY_SIZE(curbuf));
826828

827829
remove(prevbuf);

Core/GameEngine/Source/Common/System/GameMemory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2975,7 +2975,7 @@ void MemoryPoolFactory::memoryPoolUsageReport( const char* filename, FILE *appen
29752975
if( !appendToFileInstead )
29762976
{
29772977
char tmp[256];
2978-
strcpy(tmp,filename);
2978+
strlcpy(tmp, filename, ARRAY_SIZE(tmp));
29792979
strlcat(tmp, ".csv", ARRAY_SIZE(tmp));
29802980
perfStatsFile = fopen(tmp, "w");
29812981
}

Core/GameEngineDevice/Source/StdDevice/Common/StdLocalFileSystem.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ Bool StdLocalFileSystem::doesFileExist(const Char *filename) const
211211
void StdLocalFileSystem::getFileListInDirectory(const AsciiString& currentDirectory, const AsciiString& originalDirectory, const AsciiString& searchName, FilenameList & filenameList, Bool searchSubdirectories) const
212212
{
213213

214-
char search[_MAX_PATH];
215214
AsciiString asciisearch;
216215
asciisearch = originalDirectory;
217216
asciisearch.concat(currentDirectory);
@@ -227,17 +226,15 @@ void StdLocalFileSystem::getFileListInDirectory(const AsciiString& currentDirect
227226
std::replace(fixedDirectory.begin(), fixedDirectory.end(), '\\', '/');
228227
#endif
229228

230-
strcpy(search, fixedDirectory.c_str());
231-
232229
Bool done = FALSE;
233230
std::error_code ec;
234231

235-
auto iter = std::filesystem::directory_iterator(search, ec);
232+
auto iter = std::filesystem::directory_iterator(fixedDirectory.c_str(), ec);
236233
// The default iterator constructor creates an end iterator
237234
done = iter == std::filesystem::directory_iterator();
238235

239236
if (ec) {
240-
DEBUG_LOG(("StdLocalFileSystem::getFileListInDirectory - Error opening directory %s", search));
237+
DEBUG_LOG(("StdLocalFileSystem::getFileListInDirectory - Error opening directory %s", fixedDirectory.c_str()));
241238
return;
242239
}
243240

Core/GameEngineDevice/Source/Win32Device/Common/Win32LocalFileSystem.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,14 @@ void Win32LocalFileSystem::getFileListInDirectory(const AsciiString& currentDire
127127
HANDLE fileHandle = NULL;
128128
WIN32_FIND_DATA findData;
129129

130-
char search[_MAX_PATH];
131130
AsciiString asciisearch;
132131
asciisearch = originalDirectory;
133132
asciisearch.concat(currentDirectory);
134133
asciisearch.concat(searchName);
135-
strcpy(search, asciisearch.str());
136134

137135
Bool done = FALSE;
138136

139-
fileHandle = FindFirstFile(search, &findData);
137+
fileHandle = FindFirstFile(asciisearch.str(), &findData);
140138
done = (fileHandle == INVALID_HANDLE_VALUE);
141139

142140
while (!done) {

Core/Libraries/Source/WWVegas/WW3D2/collect.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,8 @@ WW3DErrorType CollectionDefClass::Load(ChunkLoadClass & cload)
10151015
if (cload.Read(&header,sizeof(header)) != sizeof(header)) goto Error;
10161016
if (!cload.Close_Chunk()) goto Error;
10171017

1018-
strlcpy(Name,header.Name,W3D_NAME_LEN);
1018+
static_assert(ARRAY_SIZE(Name) >= ARRAY_SIZE(header.Name), "Incorrect array size");
1019+
strcpy(Name,header.Name);
10191020
ObjectNames.Resize(header.RenderObjectCount);
10201021

10211022
while (cload.Open_Chunk()) {

Core/Libraries/Source/WWVegas/WW3D2/hcanim.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,17 @@ int HCompressedAnimClass::Load_W3D(ChunkLoadClass & cload)
255255

256256
cload.Close_Chunk();
257257

258-
strcpy(Name,aheader.HierarchyName);
258+
static_assert(ARRAY_SIZE(Name) >= ARRAY_SIZE(aheader.HierarchyName), "Incorrect array size");
259+
strcpy(Name, aheader.HierarchyName);
259260
strlcat(Name, ".", ARRAY_SIZE(Name));
260261
strlcat(Name, aheader.Name, ARRAY_SIZE(Name));
261262

262263
// TSS chasing crash bug 05/26/99
263264
WWASSERT(HierarchyName != NULL);
264265
WWASSERT(aheader.HierarchyName != NULL);
265266
WWASSERT(sizeof(HierarchyName) >= W3D_NAME_LEN);
266-
strlcpy(HierarchyName,aheader.HierarchyName,W3D_NAME_LEN);
267+
static_assert(ARRAY_SIZE(HierarchyName) >= ARRAY_SIZE(aheader.HierarchyName), "Incorrect array size");
268+
strcpy(HierarchyName, aheader.HierarchyName);
267269

268270
HTreeClass * base_pose = WW3DAssetManager::Get_Instance()->Get_HTree(HierarchyName);
269271
if (base_pose == NULL) {

Core/Libraries/Source/WWVegas/WW3D2/hmdldef.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ int HModelDefClass::Load_W3D(ChunkLoadClass & cload)
148148
/*
149149
** process the header info
150150
*/
151-
strlcpy(ModelName,header.Name,W3D_NAME_LEN);
152-
strlcpy(BasePoseName,header.HierarchyName,W3D_NAME_LEN);
153-
strcpy(Name,ModelName);
151+
static_assert(ARRAY_SIZE(ModelName) >= ARRAY_SIZE(header.Name), "Incorrect array size");
152+
static_assert(ARRAY_SIZE(BasePoseName) >= ARRAY_SIZE(header.HierarchyName), "Incorrect array size");
153+
static_assert(ARRAY_SIZE(Name) >= ARRAY_SIZE(ModelName), "Incorrect array size");
154+
strcpy(ModelName, header.Name);
155+
strcpy(BasePoseName, header.HierarchyName);
156+
strcpy(Name, ModelName);
154157

155158
/*
156159
** Just allocate a node for the number of sub objects we're expecting
@@ -232,7 +235,8 @@ bool HModelDefClass::read_connection(ChunkLoadClass & cload,HmdlNodeDefStruct *
232235
return false;
233236
}
234237

235-
strcpy(node->RenderObjName,ModelName);
238+
static_assert(ARRAY_SIZE(node->RenderObjName) >= ARRAY_SIZE(ModelName), "Incorrect array size");
239+
strcpy(node->RenderObjName, ModelName);
236240
strlcat(node->RenderObjName, ".", ARRAY_SIZE(node->RenderObjName));
237241
strlcat(node->RenderObjName, con.RenderObjName, ARRAY_SIZE(node->RenderObjName));
238242

Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ RingPrototypeClass::RingPrototypeClass (void)
12061206
RingPrototypeClass::RingPrototypeClass(RingRenderObjClass *ring)
12071207
{
12081208
::memset (&Definition, 0, sizeof (Definition));
1209-
::strcpy (Definition.Name, ring->Get_Name ());
1209+
strlcpy(Definition.Name, ring->Get_Name(), ARRAY_SIZE(Definition.Name));
12101210

12111211
Definition.AnimDuration = ring->AnimDuration;
12121212
Definition.Attributes = ring->Get_Flags ();
@@ -1237,7 +1237,7 @@ RingPrototypeClass::RingPrototypeClass(RingRenderObjClass *ring)
12371237
filename = name;
12381238
}
12391239

1240-
::strcpy (Definition.TextureName, filename);
1240+
strlcpy(Definition.TextureName, filename, ARRAY_SIZE(Definition.TextureName));
12411241
}
12421242

12431243
//

Core/Libraries/Source/WWVegas/WW3D2/sphereobj.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ SpherePrototypeClass::SpherePrototypeClass (void)
11621162
SpherePrototypeClass::SpherePrototypeClass(SphereRenderObjClass *sphere)
11631163
{
11641164
::memset (&Definition, 0, sizeof (Definition));
1165-
::strcpy (Definition.Name, sphere->Get_Name ());
1165+
strlcpy(Definition.Name, sphere->Get_Name(), ARRAY_SIZE(Definition.Name));
11661166

11671167
Definition.DefaultAlpha = sphere->Get_Default_Alpha ();
11681168
Definition.AnimDuration = sphere->AnimDuration;
@@ -1190,7 +1190,7 @@ SpherePrototypeClass::SpherePrototypeClass(SphereRenderObjClass *sphere)
11901190
filename = name;
11911191
}
11921192

1193-
::strcpy (Definition.TextureName, filename);
1193+
strlcpy(Definition.TextureName, filename, ARRAY_SIZE(Definition.TextureName));
11941194

11951195
}
11961196

Core/Libraries/Source/WWVegas/WWDownload/FTP.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ HRESULT Cftp::FileRecoveryPosition( LPCSTR szLocalFileName, LPCSTR szRegistryRo
17191719
17201720
// Concatenate the registry key together
17211721
1722-
strcpy( regkey, szRegistryRoot );
1722+
strlcpy(regkey, szRegistryRoot, ARRAY_SIZE(regkey));
17231723
if( regkey[ strlen( regkey ) - 1 ] != '\\' )
17241724
{
17251725
strlcat(regkey, "\\Download", ARRAY_SIZE(regkey));

0 commit comments

Comments
 (0)