From 05f3e390ac5c27bf792d76b85392efd554e6ba92 Mon Sep 17 00:00:00 2001 From: Skyaero <21192585+Skyaero42@users.noreply.github.com> Date: Tue, 4 Nov 2025 21:30:16 +0100 Subject: [PATCH 1/5] fix: replace strcpy with strlcpy and update length assertion --- Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp | 4 ++-- Core/Libraries/Source/WWVegas/WW3D2/sphereobj.cpp | 4 ++-- Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp | 10 +++++----- Core/Libraries/Source/WWVegas/WWLib/thread.cpp | 4 ++-- .../Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp | 4 ++-- .../Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp b/Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp index f5fc0417a2..dc3de46a5e 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp @@ -502,8 +502,8 @@ const char * RingRenderObjClass::Get_Name(void) const void RingRenderObjClass::Set_Name(const char * name) { WWASSERT(name != NULL); - WWASSERT(strlen(name) < 2*W3D_NAME_LEN); - strcpy(Name,name); + const size_t nameLen = strlcpy(Name, name, ARRAY_SIZE(Name)); + (void)nameLen; WWASSERT(nameLen < 2 * W3D_NAME_LEN); } /*********************************************************************************************** diff --git a/Core/Libraries/Source/WWVegas/WW3D2/sphereobj.cpp b/Core/Libraries/Source/WWVegas/WW3D2/sphereobj.cpp index bd3514e747..2e97045991 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/sphereobj.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/sphereobj.cpp @@ -437,8 +437,8 @@ const char * SphereRenderObjClass::Get_Name(void) const void SphereRenderObjClass::Set_Name(const char * name) { WWASSERT(name != NULL); - WWASSERT(strlen(name) < 2*W3D_NAME_LEN); - strcpy(Name,name); + const size_t nameLen = strlcpy(Name, name, ARRAY_SIZE(Name)); + (void)nameLen; WWASSERT(nameLen < ARRAY_SIZE(Name)); } diff --git a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp index 81659faabf..9bee70a6e8 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp @@ -532,9 +532,9 @@ static void Get_W3D_Name (const char *filename, char *w3d_name) // Copy all characters from start to end (excluding 'end') // into the w3d_name buffer. Then capitalize the string. - int num_chars = end - start; - WWASSERT(num_chars <= W3D_NAME_LEN); - strlcpy(w3d_name, start, min(W3D_NAME_LEN, num_chars)); + size_t num_chars = end - start; + const size_t nameLen = strlcpy(w3d_name, start, W3D_NAME_LEN); + (void)nameLen; WWASSERT(nameLen < num_chars); strupr(w3d_name); } @@ -554,7 +554,6 @@ static void Get_W3D_Name (const char *filename, char *w3d_name) static const char * Make_W3D_Filename (const char *w3d_name) { assert(w3d_name); - assert(strlen(w3d_name) < W3D_NAME_LEN); // Copy the w3d name into a static buffer, turn it into lowercase // 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) buffer[0] = 0; return buffer; } - strcpy(buffer, w3d_name); + const size_t bufferLen = strlcpy(buffer, w3d_name, ARRAY_SIZE(buffer)); + (void)bufferLen; WWASSERT(bufferLen < W3D_NAME_LEN); char *dot = strchr(buffer, '.'); if (dot) *dot = 0; diff --git a/Core/Libraries/Source/WWVegas/WWLib/thread.cpp b/Core/Libraries/Source/WWVegas/WWLib/thread.cpp index d6cefc030b..6cbb50d84d 100644 --- a/Core/Libraries/Source/WWVegas/WWLib/thread.cpp +++ b/Core/Libraries/Source/WWVegas/WWLib/thread.cpp @@ -34,8 +34,8 @@ ThreadClass::ThreadClass(const char *thread_name, ExceptionHandlerType exception_handler) : handle(0), running(false), thread_priority(0) { if (thread_name) { - assert(strlen(thread_name) < sizeof(ThreadName) - 1); - strcpy(ThreadName, thread_name); + size_t nameLen = strlcpy(ThreadName, thread_name, ARRAY_SIZE(ThreadName)); + (void)nameLen; assert(nameLen < ARRAY_SIZE(ThreadName)); } else { strcpy(ThreadName, "No name");; } diff --git a/Generals/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp b/Generals/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp index 66b6b8e4bd..829cb1406c 100644 --- a/Generals/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp +++ b/Generals/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp @@ -307,8 +307,8 @@ const char * BoxRenderObjClass::Get_Name(void) const void BoxRenderObjClass::Set_Name(const char * name) { WWASSERT(name != NULL); - WWASSERT(strlen(name) < 2*W3D_NAME_LEN); - strcpy(Name,name); + size_t nameLen = strlcpy(Name, name, ARRAY_SIZE(Name)); + (void)nameLen; WWASSERT(nameLen < ARRAY_SIZE(Name)); } diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp index 5c72eab346..f2bb6637b5 100644 --- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp +++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/boxrobj.cpp @@ -307,8 +307,8 @@ const char * BoxRenderObjClass::Get_Name(void) const void BoxRenderObjClass::Set_Name(const char * name) { WWASSERT(name != NULL); - WWASSERT(strlen(name) < 2*W3D_NAME_LEN); - strcpy(Name,name); + size_t nameLen = strlcpy(Name, name, ARRAY_SIZE(Name)); + (void)nameLen; WWASSERT(nameLen < ARRAY_SIZE(Name)); } From 3e3c3099061e117d383d39ed8d8772a0b83f5e61 Mon Sep 17 00:00:00 2001 From: Skyaero <21192585+Skyaero42@users.noreply.github.com> Date: Wed, 5 Nov 2025 09:16:24 +0100 Subject: [PATCH 2/5] Changes based on review xezon 2025-11-04 --- Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp | 2 +- Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp b/Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp index dc3de46a5e..6fde0831d2 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/ringobj.cpp @@ -503,7 +503,7 @@ void RingRenderObjClass::Set_Name(const char * name) { WWASSERT(name != NULL); const size_t nameLen = strlcpy(Name, name, ARRAY_SIZE(Name)); - (void)nameLen; WWASSERT(nameLen < 2 * W3D_NAME_LEN); + (void)nameLen; WWASSERT(nameLen < ARRAY_SIZE(Name)); } /*********************************************************************************************** diff --git a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp index 9bee70a6e8..27a4d01412 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp @@ -533,8 +533,9 @@ static void Get_W3D_Name (const char *filename, char *w3d_name) // Copy all characters from start to end (excluding 'end') // into the w3d_name buffer. Then capitalize the string. size_t num_chars = end - start; - const size_t nameLen = strlcpy(w3d_name, start, W3D_NAME_LEN); - (void)nameLen; WWASSERT(nameLen < num_chars); + const size_t strLen = end - start; + WWASSERT(strLen < W3D_NAME_LEN); + strlcpy(w3d_name, start, W3D_NAME_LEN); strupr(w3d_name); } From 8b09a791c63fbb4fe52e1826e2bead7a468a95c8 Mon Sep 17 00:00:00 2001 From: Skyaero <21192585+Skyaero42@users.noreply.github.com> Date: Wed, 5 Nov 2025 20:46:56 +0100 Subject: [PATCH 3/5] undo Get_W3D_Name change --- Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp index 27a4d01412..21c517a41e 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp @@ -532,10 +532,9 @@ static void Get_W3D_Name (const char *filename, char *w3d_name) // Copy all characters from start to end (excluding 'end') // into the w3d_name buffer. Then capitalize the string. - size_t num_chars = end - start; - const size_t strLen = end - start; - WWASSERT(strLen < W3D_NAME_LEN); - strlcpy(w3d_name, start, W3D_NAME_LEN); + int num_chars = end - start; + WWASSERT(num_chars <= W3D_NAME_LEN); + strlcpy(w3d_name, start, min(W3D_NAME_LEN, num_chars)); strupr(w3d_name); } From cd012533b8260563dac9a342c53e0eb2f2759724 Mon Sep 17 00:00:00 2001 From: Skyaero <21192585+Skyaero42@users.noreply.github.com> Date: Thu, 6 Nov 2025 09:07:14 +0100 Subject: [PATCH 4/5] revisiting w3d_dep --- Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp index 21c517a41e..3cac876d3a 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp @@ -83,7 +83,7 @@ static void Scan_Emitter (ChunkLoadClass &cload, StringList &files, const char * static void Scan_Aggregate (ChunkLoadClass &cload, StringList &files, const char *w3d_name); static void Scan_HLOD (ChunkLoadClass &cload, StringList &files, const char *w3d_name); -static void Get_W3D_Name (const char *filename, char *w3d_name); +static void Get_W3D_Name (const char *filename, char *w3d_name, size_t w3d_name_size); static const char * Make_W3D_Filename (const char *w3d_name); @@ -118,7 +118,7 @@ bool Get_W3D_Dependencies (const char *w3d_filename, StringList &files) // Get the W3D name from the filename. char w3d_name[W3D_NAME_LEN]; - Get_W3D_Name(w3d_filename, w3d_name); + Get_W3D_Name(w3d_filename, w3d_name, W3D_NAME_LEN); // Create a chunk loader for this file, and scan the file. ChunkLoadClass cload(file); @@ -511,7 +511,7 @@ static void Scan_HLOD (ChunkLoadClass &cload, StringList &files, const char *w3d * HISTORY: * * 4/3/00 AJA : Created. * *=============================================================================================*/ -static void Get_W3D_Name (const char *filename, char *w3d_name) +static void Get_W3D_Name(const char* filename, char* w3d_name, size_t w3d_name_size) { assert(filename); assert(w3d_name); @@ -532,9 +532,9 @@ static void Get_W3D_Name (const char *filename, char *w3d_name) // Copy all characters from start to end (excluding 'end') // into the w3d_name buffer. Then capitalize the string. - int num_chars = end - start; - WWASSERT(num_chars <= W3D_NAME_LEN); - strlcpy(w3d_name, start, min(W3D_NAME_LEN, num_chars)); + size_t num_chars = end - start; + WWASSERT(num_chars < w3d_name_size); + strlcpy(w3d_name, start, min(w3d_name_size, num_chars)); strupr(w3d_name); } From 111ea3c28362b2fac185b863ab4683083831b43a Mon Sep 17 00:00:00 2001 From: Skyaero <21192585+Skyaero42@users.noreply.github.com> Date: Thu, 6 Nov 2025 09:29:47 +0100 Subject: [PATCH 5/5] fix array size --- Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp index 3cac876d3a..415b9fb8c5 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/w3d_dep.cpp @@ -118,7 +118,7 @@ bool Get_W3D_Dependencies (const char *w3d_filename, StringList &files) // Get the W3D name from the filename. char w3d_name[W3D_NAME_LEN]; - Get_W3D_Name(w3d_filename, w3d_name, W3D_NAME_LEN); + Get_W3D_Name(w3d_filename, w3d_name, ARRAY_SIZE(w3d_name)); // Create a chunk loader for this file, and scan the file. ChunkLoadClass cload(file);