Skip to content

Commit f5c8fab

Browse files
authored
refactor: Convert strlcpy to strcpy with static_assert (#1887)
1 parent a2c1d0e commit f5c8fab

File tree

10 files changed

+26
-22
lines changed

10 files changed

+26
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ long Targa::Save(const char* name, long flags, bool addextension)
713713
if (!error) {
714714

715715
mExtension.ExtSize = 495;
716-
strlcpy(mExtension.SoftID, "Denzil's Targa Code", sizeof(mExtension.SoftID));
716+
strcpy(mExtension.SoftID, "Denzil's Targa Code");
717717
mExtension.SoftVer.Number = (1 * 100);
718718
mExtension.SoftVer.Letter = 0;
719719

Generals/Code/GameEngine/Source/Common/INI/INI.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,8 @@ UnsignedInt INI::load( AsciiString filename, INILoadType loadType, Xfer *pXfer )
404404
if (parse)
405405
{
406406
#ifdef DEBUG_CRASHING
407-
strlcpy(m_curBlockStart, m_buffer, ARRAY_SIZE(m_curBlockStart));
407+
static_assert(ARRAY_SIZE(m_curBlockStart) >= ARRAY_SIZE(m_buffer), "Incorrect array size");
408+
strcpy(m_curBlockStart, m_buffer);
408409
#endif
409410
try {
410411
(*parse)( this );

Generals/Code/GameEngine/Source/Common/PerfTimer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ void PerfGather::reset()
272272
strlcpy(s_buf, fname, ARRAY_SIZE(s_buf);
273273

274274
char tmp[256];
275-
strlcpy(tmp, s_buf, ARRAY_SIZE(tmp));
275+
static_assert(ARRAY_SIZE(tmp) >= ARRAY_SIZE(s_buf), "Incorrect array size");
276+
strcpy(tmp, s_buf);
276277
strlcat(tmp, ".csv", ARRAY_SIZE(tmp));
277278

278279
s_perfStatsFile = fopen(tmp, "w");

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,10 @@ int HMorphAnimClass::Save_W3D(ChunkSaveClass & csave)
623623

624624
// init the header data
625625
W3dMorphAnimHeaderStruct header;
626-
strlcpy(header.Name,AnimName,sizeof(header.Name));
627-
strlcpy(header.HierarchyName,HierarchyName,sizeof(header.HierarchyName));
626+
static_assert(ARRAY_SIZE(header.Name) >= ARRAY_SIZE(AnimName), "Incorrect array size");
627+
static_assert(ARRAY_SIZE(header.HierarchyName) >= ARRAY_SIZE(HierarchyName), "Incorrect array size");
628+
strcpy(header.Name, AnimName);
629+
strcpy(header.HierarchyName, HierarchyName);
628630

629631
header.FrameCount = FrameCount;
630632
header.FrameRate = FrameRate;

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,13 @@ int HRawAnimClass::Load_W3D(ChunkLoadClass & cload)
211211
pre30 = true;
212212
}
213213

214-
strlcpy(Name, aheader.HierarchyName, ARRAY_SIZE(Name));
214+
static_assert(ARRAY_SIZE(Name) >= ARRAY_SIZE(aheader.HierarchyName), "Incorrect array size");
215+
strcpy(Name, aheader.HierarchyName);
215216
strlcat(Name, ".", ARRAY_SIZE(Name));
216217
strlcat(Name, aheader.Name, ARRAY_SIZE(Name));
217218

218-
// TSS chasing crash bug 05/26/99
219-
WWASSERT(HierarchyName != NULL);
220-
WWASSERT(aheader.HierarchyName != NULL);
221-
WWASSERT(sizeof(HierarchyName) >= W3D_NAME_LEN);
222-
strlcpy(HierarchyName,aheader.HierarchyName,W3D_NAME_LEN);
219+
static_assert(ARRAY_SIZE(HierarchyName) >= ARRAY_SIZE(aheader.HierarchyName), "Incorrect array size");
220+
strcpy(HierarchyName, aheader.HierarchyName);
223221

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

GeneralsMD/Code/GameEngine/Include/Common/INI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,6 @@ class INI
420420
const char *m_blockEndToken; ///< token to represent end of data block
421421
Bool m_endOfFile; ///< TRUE when we've hit EOF
422422
#ifdef DEBUG_CRASHING
423-
char m_curBlockStart[ INI_MAX_CHARS_PER_LINE ]; ///< first line of cur block
423+
char m_curBlockStart[ INI_MAX_CHARS_PER_LINE+1 ]; ///< first line of cur block
424424
#endif
425425
};

GeneralsMD/Code/GameEngine/Source/Common/INI/INI.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ UnsignedInt INI::load( AsciiString filename, INILoadType loadType, Xfer *pXfer )
411411
if (parse)
412412
{
413413
#ifdef DEBUG_CRASHING
414-
strlcpy(m_curBlockStart, m_buffer, ARRAY_SIZE(m_curBlockStart));
414+
static_assert(ARRAY_SIZE(m_curBlockStart) >= ARRAY_SIZE(m_buffer), "Incorrect array size");
415+
strcpy(m_curBlockStart, m_buffer);
415416
#endif
416417
try {
417418
(*parse)( this );

GeneralsMD/Code/GameEngine/Source/Common/PerfTimer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ void PerfGather::reset()
352352
strlcpy(s_buf, fname, ARRAY_SIZE(s_buf);
353353

354354
char tmp[256];
355-
strlcpy(tmp, s_buf, ARRAY_SIZE(tmp));
355+
static_assert(ARRAY_SIZE(tmp) >= ARRAY_SIZE(s_buf), "Incorrect array size");
356+
strcpy(tmp, s_buf);
356357
strlcat(tmp, ".csv", ARRAY_SIZE(tmp));
357358

358359
s_perfStatsFile = fopen(tmp, "w");

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,10 @@ int HMorphAnimClass::Save_W3D(ChunkSaveClass & csave)
622622

623623
// init the header data
624624
W3dMorphAnimHeaderStruct header;
625-
strlcpy(header.Name,AnimName,sizeof(header.Name));
626-
strlcpy(header.HierarchyName,HierarchyName,sizeof(header.HierarchyName));
625+
static_assert(ARRAY_SIZE(header.Name) >= ARRAY_SIZE(AnimName), "Incorrect array size");
626+
static_assert(ARRAY_SIZE(header.HierarchyName) >= ARRAY_SIZE(HierarchyName), "Incorrect array size");
627+
strcpy(header.Name, AnimName);
628+
strcpy(header.HierarchyName, HierarchyName);
627629

628630
header.FrameCount = FrameCount;
629631
header.FrameRate = FrameRate;

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,13 @@ int HRawAnimClass::Load_W3D(ChunkLoadClass & cload)
211211
pre30 = true;
212212
}
213213

214-
strlcpy(Name, aheader.HierarchyName, ARRAY_SIZE(Name));
214+
static_assert(ARRAY_SIZE(Name) >= ARRAY_SIZE(aheader.HierarchyName), "Incorrect array size");
215+
strcpy(Name, aheader.HierarchyName);
215216
strlcat(Name, ".", ARRAY_SIZE(Name));
216217
strlcat(Name, aheader.Name, ARRAY_SIZE(Name));
217218

218-
// TSS chasing crash bug 05/26/99
219-
WWASSERT(HierarchyName != NULL);
220-
WWASSERT(aheader.HierarchyName != NULL);
221-
WWASSERT(sizeof(HierarchyName) >= W3D_NAME_LEN);
222-
strlcpy(HierarchyName,aheader.HierarchyName,W3D_NAME_LEN);
219+
static_assert(ARRAY_SIZE(HierarchyName) >= ARRAY_SIZE(aheader.HierarchyName), "Incorrect array size");
220+
strcpy(HierarchyName, aheader.HierarchyName);
223221

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

0 commit comments

Comments
 (0)