Skip to content

Commit 630f6df

Browse files
authored
fix: Replace strncpy with strlcpy for robustness (#1533)
1 parent 9ede47f commit 630f6df

File tree

58 files changed

+158
-202
lines changed

Some content is hidden

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

58 files changed

+158
-202
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ 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-
strncpy(Name,header.Name,W3D_NAME_LEN);
1018+
strlcpy(Name,header.Name,W3D_NAME_LEN);
10191019
ObjectNames.Resize(header.RenderObjectCount);
10201020

10211021
while (cload.Open_Chunk()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ int HCompressedAnimClass::Load_W3D(ChunkLoadClass & cload)
264264
WWASSERT(HierarchyName != NULL);
265265
WWASSERT(aheader.HierarchyName != NULL);
266266
WWASSERT(sizeof(HierarchyName) >= W3D_NAME_LEN);
267-
strncpy(HierarchyName,aheader.HierarchyName,W3D_NAME_LEN);
267+
strlcpy(HierarchyName,aheader.HierarchyName,W3D_NAME_LEN);
268268

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

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,8 @@ int HModelDefClass::Load_W3D(ChunkLoadClass & cload)
149149
/*
150150
** process the header info
151151
*/
152-
strncpy(ModelName,header.Name,W3D_NAME_LEN);
153-
ModelName[W3D_NAME_LEN - 1] = 0;
154-
strncpy(BasePoseName,header.HierarchyName,W3D_NAME_LEN);
155-
BasePoseName[W3D_NAME_LEN-1] = 0;
152+
strlcpy(ModelName,header.Name,W3D_NAME_LEN);
153+
strlcpy(BasePoseName,header.HierarchyName,W3D_NAME_LEN);
156154
strcpy(Name,ModelName);
157155

158156
/*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
memset(w3d_name, 0, W3D_NAME_LEN); // blank out the buffer
536535
int num_chars = end - start;
537-
strncpy(w3d_name, start, num_chars < W3D_NAME_LEN ? num_chars : W3D_NAME_LEN-1);
536+
WWASSERT(num_chars <= W3D_NAME_LEN);
537+
strlcpy(w3d_name, start, min(W3D_NAME_LEN, num_chars));
538538
strupr(w3d_name);
539539
}
540540

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// Download.cpp : Implementation of CDownload
2020
#include "DownloadDebug.h"
2121
#include "Download.h"
22+
#include "stringex.h"
2223
#include <mmsystem.h>
2324
#include <assert.h>
2425
#include <direct.h>
@@ -69,15 +70,15 @@ HRESULT CDownload::DownloadFile(LPCSTR server, LPCSTR username, LPCSTR password,
6970
_mkdir("download");
7071

7172
// Copy parameters to member variables.
72-
strncpy( m_Server, server, sizeof( m_Server ) );
73-
strncpy( m_Login, username, sizeof( m_Login ) );
74-
strncpy( m_Password, password, sizeof( m_Password ) );
75-
strncpy( m_File, file, sizeof( m_File ) );
76-
strncpy( m_LocalFile, localfile, sizeof( m_LocalFile ) );
73+
strlcpy( m_Server, server, sizeof( m_Server ) );
74+
strlcpy( m_Login, username, sizeof( m_Login ) );
75+
strlcpy( m_Password, password, sizeof( m_Password ) );
76+
strlcpy( m_File, file, sizeof( m_File ) );
77+
strlcpy( m_LocalFile, localfile, sizeof( m_LocalFile ) );
7778

78-
strncpy( m_LastLocalFile, localfile, sizeof( m_LastLocalFile ) );
79+
strlcpy( m_LastLocalFile, localfile, sizeof( m_LastLocalFile ) );
7980

80-
strncpy( m_RegKey, regkey, sizeof( m_RegKey ) );
81+
strlcpy( m_RegKey, regkey, sizeof( m_RegKey ) );
8182
m_TryResume = tryresume;
8283
m_StartPosition=0;
8384

@@ -113,8 +114,7 @@ HRESULT CDownload::GetLastLocalFile(char *local_file, int maxlen) {
113114
if (local_file==0)
114115
return(E_FAIL);
115116

116-
strncpy(local_file, m_LastLocalFile, maxlen);
117-
local_file[maxlen-1]=0;
117+
strlcpy(local_file, m_LastLocalFile, maxlen);
118118

119119
return(S_OK);
120120
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <errno.h>
3939
//#include "wlib/wstring.h"
4040

41+
#include "stringex.h"
4142
#include "DownloadDebug.h"
4243

4344
// umm... what?? BGC 3/27/03
@@ -324,7 +325,7 @@ HRESULT Cftp::ConnectToServer(LPCSTR szServerName)
324325
char buffer[ 256 ];
325326
int iReply, error;
326327

327-
strncpy( m_szServerName, szServerName, 128 );
328+
strlcpy( m_szServerName, szServerName, sizeof(m_szServerName));
328329

329330
if( m_iStatus == FTPSTAT_INIT )
330331
{
@@ -487,8 +488,8 @@ HRESULT Cftp::LoginToServer( LPCSTR szUserName, LPCSTR szPassword )
487488
char command[ 256 ];
488489
int iReply;
489490

490-
strncpy( m_szUserName, szUserName, 128 );
491-
strncpy( m_szPassword, szPassword, 128 );
491+
strlcpy( m_szUserName, szUserName, sizeof(m_szUserName));
492+
strlcpy( m_szPassword, szPassword, sizeof(m_szPassword));
492493

493494
memset( command, 0, 256 );
494495

@@ -1396,7 +1397,7 @@ HRESULT Cftp::GetNextFileBlock( LPCSTR szLocalFileName, int * piTotalRead )
13961397

13971398
//char str[ 256 ];
13981399

1399-
strncpy( m_szLocalFileName, szLocalFileName, 256 );
1400+
strlcpy( m_szLocalFileName, szLocalFileName, sizeof(m_szLocalFileName));
14001401

14011402
// Open local file
14021403

@@ -1830,8 +1831,7 @@ bool Prepare_Directories(const char *rootdir, const char *filename)
18301831
const char *cptr=filename;
18311832
while(cptr=strchr(cptr,'\\'))
18321833
{
1833-
memset(tempstr,0,256);
1834-
strncpy(tempstr,filename,cptr-filename);
1834+
strlcpy(tempstr,filename,cptr-filename + 1);
18351835
sprintf(newdir,"%s\\%s",rootdir, tempstr);
18361836
if (!CreateDirectory(newdir, NULL))
18371837
return false;

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

Lines changed: 2 additions & 2 deletions
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-
strncpy(mExtension.SoftID, "Denzil's Targa Code", 41);
716+
strlcpy(mExtension.SoftID, "Denzil's Targa Code", sizeof(mExtension.SoftID));
717717
mExtension.SoftVer.Number = (1 * 100);
718718
mExtension.SoftVer.Letter = 0;
719719

@@ -735,7 +735,7 @@ long Targa::Save(const char* name, long flags, bool addextension)
735735
if (!error)
736736
{
737737
footer.Developer = 0;
738-
strncpy(footer.Signature, TGA2_SIGNATURE, 16);
738+
strlcpy(footer.Signature, TGA2_SIGNATURE, sizeof(footer.Signature));
739739
footer.RsvdChar = '.';
740740
footer.BZST = 0;
741741

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ void CPUDetectClass::Init_Processor_String()
802802
case INTEL_PROCESSOR_PENTIUM4: str+="Pentium4"; break;
803803
}
804804
}
805-
strncpy(ProcessorString,str.str(),sizeof(ProcessorString));
805+
strlcpy(ProcessorString,str.str(),sizeof(ProcessorString));
806806
}
807807

808808
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,7 @@ bool INIClass::Put_TextBlock(char const * section, char const * text)
11501150

11511151
char buffer[128];
11521152

1153-
strncpy(buffer, text, 75);
1154-
buffer[75] = '\0';
1153+
strlcpy(buffer, text, 76);
11551154

11561155
char b[32];
11571156
sprintf(b, "%d", index);
@@ -1724,8 +1723,7 @@ int INIClass::Get_String(char const * section, char const * entry, char const *
17241723
buffer[0] = '\0';
17251724
return(0);
17261725
} else {
1727-
strncpy(buffer, defvalue, size);
1728-
buffer[size-1] = '\0';
1726+
strlcpy(buffer, defvalue, size);
17291727
strtrim(buffer);
17301728
return(strlen(buffer));
17311729
}

Core/Libraries/Source/debug/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ target_include_directories(core_debug INTERFACE
3434

3535
target_link_libraries(core_debug PRIVATE
3636
core_config
37+
core_wwcommon
3738
corei_always
3839
)

0 commit comments

Comments
 (0)