Skip to content

Commit ff65d33

Browse files
authored
fix: Replace strcpy with strlcpy and update length asserts (#1796)
1 parent c1f26a5 commit ff65d33

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ const char * RingRenderObjClass::Get_Name(void) const
502502
void RingRenderObjClass::Set_Name(const char * name)
503503
{
504504
WWASSERT(name != NULL);
505-
WWASSERT(strlen(name) < 2*W3D_NAME_LEN);
506-
strcpy(Name,name);
505+
const size_t nameLen = strlcpy(Name, name, ARRAY_SIZE(Name));
506+
(void)nameLen; WWASSERT(nameLen < ARRAY_SIZE(Name));
507507
}
508508

509509
/***********************************************************************************************

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,8 @@ const char * SphereRenderObjClass::Get_Name(void) const
437437
void SphereRenderObjClass::Set_Name(const char * name)
438438
{
439439
WWASSERT(name != NULL);
440-
WWASSERT(strlen(name) < 2*W3D_NAME_LEN);
441-
strcpy(Name,name);
440+
const size_t nameLen = strlcpy(Name, name, ARRAY_SIZE(Name));
441+
(void)nameLen; WWASSERT(nameLen < ARRAY_SIZE(Name));
442442
}
443443

444444

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static void Scan_Emitter (ChunkLoadClass &cload, StringList &files, const char *
8383
static void Scan_Aggregate (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
8484
static void Scan_HLOD (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
8585

86-
static void Get_W3D_Name (const char *filename, char *w3d_name);
86+
static void Get_W3D_Name (const char *filename, char *w3d_name, size_t w3d_name_size);
8787
static const char * Make_W3D_Filename (const char *w3d_name);
8888

8989

@@ -118,7 +118,7 @@ bool Get_W3D_Dependencies (const char *w3d_filename, StringList &files)
118118

119119
// Get the W3D name from the filename.
120120
char w3d_name[W3D_NAME_LEN];
121-
Get_W3D_Name(w3d_filename, w3d_name);
121+
Get_W3D_Name(w3d_filename, w3d_name, ARRAY_SIZE(w3d_name));
122122

123123
// Create a chunk loader for this file, and scan the file.
124124
ChunkLoadClass cload(file);
@@ -511,7 +511,7 @@ static void Scan_HLOD (ChunkLoadClass &cload, StringList &files, const char *w3d
511511
* HISTORY: *
512512
* 4/3/00 AJA : Created. *
513513
*=============================================================================================*/
514-
static void Get_W3D_Name (const char *filename, char *w3d_name)
514+
static void Get_W3D_Name(const char* filename, char* w3d_name, size_t w3d_name_size)
515515
{
516516
assert(filename);
517517
assert(w3d_name);
@@ -532,9 +532,9 @@ static void Get_W3D_Name (const char *filename, char *w3d_name)
532532

533533
// Copy all characters from start to end (excluding 'end')
534534
// into the w3d_name buffer. Then capitalize the string.
535-
int num_chars = end - start;
536-
WWASSERT(num_chars <= W3D_NAME_LEN);
537-
strlcpy(w3d_name, start, min(W3D_NAME_LEN, num_chars));
535+
size_t num_chars = end - start;
536+
WWASSERT(num_chars < w3d_name_size);
537+
strlcpy(w3d_name, start, min(w3d_name_size, num_chars));
538538
strupr(w3d_name);
539539
}
540540

@@ -554,7 +554,6 @@ static void Get_W3D_Name (const char *filename, char *w3d_name)
554554
static const char * Make_W3D_Filename (const char *w3d_name)
555555
{
556556
assert(w3d_name);
557-
assert(strlen(w3d_name) < W3D_NAME_LEN);
558557

559558
// Copy the w3d name into a static buffer, turn it into lowercase
560559
// letters, and append a ".w3d" file extension. That's the filename.
@@ -565,7 +564,8 @@ static const char * Make_W3D_Filename (const char *w3d_name)
565564
buffer[0] = 0;
566565
return buffer;
567566
}
568-
strcpy(buffer, w3d_name);
567+
const size_t bufferLen = strlcpy(buffer, w3d_name, ARRAY_SIZE(buffer));
568+
(void)bufferLen; WWASSERT(bufferLen < W3D_NAME_LEN);
569569
char *dot = strchr(buffer, '.');
570570
if (dot)
571571
*dot = 0;

Core/Libraries/Source/WWVegas/WWLib/thread.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
ThreadClass::ThreadClass(const char *thread_name, ExceptionHandlerType exception_handler) : handle(0), running(false), thread_priority(0)
3535
{
3636
if (thread_name) {
37-
assert(strlen(thread_name) < sizeof(ThreadName) - 1);
38-
strcpy(ThreadName, thread_name);
37+
size_t nameLen = strlcpy(ThreadName, thread_name, ARRAY_SIZE(ThreadName));
38+
(void)nameLen; assert(nameLen < ARRAY_SIZE(ThreadName));
3939
} else {
4040
strcpy(ThreadName, "No name");;
4141
}

Generals/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ const char * BoxRenderObjClass::Get_Name(void) const
307307
void BoxRenderObjClass::Set_Name(const char * name)
308308
{
309309
WWASSERT(name != NULL);
310-
WWASSERT(strlen(name) < 2*W3D_NAME_LEN);
311-
strcpy(Name,name);
310+
size_t nameLen = strlcpy(Name, name, ARRAY_SIZE(Name));
311+
(void)nameLen; WWASSERT(nameLen < ARRAY_SIZE(Name));
312312
}
313313

314314

GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ const char * BoxRenderObjClass::Get_Name(void) const
307307
void BoxRenderObjClass::Set_Name(const char * name)
308308
{
309309
WWASSERT(name != NULL);
310-
WWASSERT(strlen(name) < 2*W3D_NAME_LEN);
311-
strcpy(Name,name);
310+
size_t nameLen = strlcpy(Name, name, ARRAY_SIZE(Name));
311+
(void)nameLen; WWASSERT(nameLen < ARRAY_SIZE(Name));
312312
}
313313

314314

0 commit comments

Comments
 (0)