From 5b9373211beb8163f87aaacd503cc9145301a0ab Mon Sep 17 00:00:00 2001 From: Jacob Ledbetter Date: Thu, 17 Sep 2026 12:45:24 -0600 Subject: [PATCH] fix(ww3d2): Copy string setter inputs before freeing aliased storage --- Core/Libraries/Source/WWVegas/WW3D2/agg_def.h | 7 ++++++- .../Libraries/Source/WWVegas/WWSaveLoad/parameter.h | 13 ++++--------- .../Libraries/Source/WWVegas/WW3D2/part_emt.cpp | 13 ++++--------- .../Libraries/Source/WWVegas/WW3D2/part_ldr.cpp | 10 ++++++---- .../Libraries/Source/WWVegas/WW3D2/part_emt.cpp | 13 ++++--------- .../Libraries/Source/WWVegas/WW3D2/part_ldr.cpp | 10 ++++++---- 6 files changed, 30 insertions(+), 36 deletions(-) diff --git a/Core/Libraries/Source/WWVegas/WW3D2/agg_def.h b/Core/Libraries/Source/WWVegas/WW3D2/agg_def.h index 97228dfb374..85c994c4d69 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/agg_def.h +++ b/Core/Libraries/Source/WWVegas/WW3D2/agg_def.h @@ -103,7 +103,12 @@ class AggregateDefClass virtual WW3DErrorType Load_W3D (ChunkLoadClass &chunk_load); virtual WW3DErrorType Save_W3D (ChunkSaveClass &chunk_save); const char * Get_Name () const { return m_pName; } - void Set_Name (const char *pname) { SAFE_FREE (m_pName); m_pName = ::_strdup (pname); } + void Set_Name (const char* pname) + { + char* name = ::_strdup(pname); + SAFE_FREE(m_pName); + m_pName = name; + } RenderObjClass * Create (); AggregateDefClass * Clone () const { return W3DNEW AggregateDefClass (*this); } diff --git a/Core/Libraries/Source/WWVegas/WWSaveLoad/parameter.h b/Core/Libraries/Source/WWVegas/WWSaveLoad/parameter.h index 18ba03ecf3a..c7838c49bad 100644 --- a/Core/Libraries/Source/WWVegas/WWSaveLoad/parameter.h +++ b/Core/Libraries/Source/WWVegas/WWSaveLoad/parameter.h @@ -208,16 +208,11 @@ ParameterClass::Get_Name () const // Set_Name ////////////////////////////////////////////////////////////////////////////////// inline void -ParameterClass::Set_Name (const char *new_name) +ParameterClass::Set_Name (const char* new_name) { - if (m_Name != nullptr) { - ::free ((void *)m_Name); - m_Name = nullptr; - } - - if (new_name != nullptr) { - m_Name = ::strdup (new_name); - } + char* name = new_name ? ::strdup(new_name) : nullptr; + ::free((void*)m_Name); + m_Name = name; } diff --git a/Generals/Code/Libraries/Source/WWVegas/WW3D2/part_emt.cpp b/Generals/Code/Libraries/Source/WWVegas/WW3D2/part_emt.cpp index 9f644336998..47dbd920d19 100644 --- a/Generals/Code/Libraries/Source/WWVegas/WW3D2/part_emt.cpp +++ b/Generals/Code/Libraries/Source/WWVegas/WW3D2/part_emt.cpp @@ -818,16 +818,11 @@ ParticleEmitterClass::Save (ChunkSaveClass &chunk_save) const void -ParticleEmitterClass::Set_Name (const char *pname) +ParticleEmitterClass::Set_Name (const char* pname) { - // Free the old name if necessary - if (NameString != nullptr) { - ::free (NameString); - NameString = nullptr; - } - - // Copy the provided name - NameString = ::_strdup (pname); + char* name = ::_strdup(pname); + ::free(NameString); + NameString = name; } diff --git a/Generals/Code/Libraries/Source/WWVegas/WW3D2/part_ldr.cpp b/Generals/Code/Libraries/Source/WWVegas/WW3D2/part_ldr.cpp index 6e36f9cfe00..af907b9a156 100644 --- a/Generals/Code/Libraries/Source/WWVegas/WW3D2/part_ldr.cpp +++ b/Generals/Code/Libraries/Source/WWVegas/WW3D2/part_ldr.cpp @@ -264,8 +264,9 @@ ParticleEmitterDefClass::Set_Creation_Volume (Vector3Randomizer *randomizer) void ParticleEmitterDefClass::Set_User_String (const char *pstring) { - SAFE_FREE (m_pUserString); - m_pUserString = ::_strdup (pstring); + char* copy = ::_strdup(pstring); + SAFE_FREE(m_pUserString); + m_pUserString = copy; } @@ -276,8 +277,9 @@ ParticleEmitterDefClass::Set_User_String (const char *pstring) void ParticleEmitterDefClass::Set_Name (const char *pname) { - SAFE_FREE (m_pName); - m_pName = ::_strdup (pname); + char* copy = ::_strdup(pname); + SAFE_FREE(m_pName); + m_pName = copy; } diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/part_emt.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/part_emt.cpp index bdab80fac63..4c2ffc9b077 100644 --- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/part_emt.cpp +++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/part_emt.cpp @@ -831,16 +831,11 @@ ParticleEmitterClass::Save (ChunkSaveClass &chunk_save) const void -ParticleEmitterClass::Set_Name (const char *pname) +ParticleEmitterClass::Set_Name (const char* pname) { - // Free the old name if necessary - if (NameString != nullptr) { - ::free (NameString); - NameString = nullptr; - } - - // Copy the provided name - NameString = ::_strdup (pname); + char* name = ::_strdup(pname); + ::free(NameString); + NameString = name; } diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/part_ldr.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/part_ldr.cpp index eae230609f1..9209bd3c9c4 100644 --- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/part_ldr.cpp +++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/part_ldr.cpp @@ -267,8 +267,9 @@ ParticleEmitterDefClass::Set_Creation_Volume (Vector3Randomizer *randomizer) void ParticleEmitterDefClass::Set_User_String (const char *pstring) { - SAFE_FREE (m_pUserString); - m_pUserString = ::_strdup (pstring); + char* copy = ::_strdup(pstring); + SAFE_FREE(m_pUserString); + m_pUserString = copy; } @@ -279,8 +280,9 @@ ParticleEmitterDefClass::Set_User_String (const char *pstring) void ParticleEmitterDefClass::Set_Name (const char *pname) { - SAFE_FREE (m_pName); - m_pName = ::_strdup (pname); + char* copy = ::_strdup(pname); + SAFE_FREE(m_pName); + m_pName = copy; }