Use idStr::Copynz() instead of strncpy()

to guarantee \0-termination
This commit is contained in:
Daniel Gibson 2022-01-06 20:39:09 +01:00
parent df0916ecf8
commit ea781c577e
14 changed files with 19 additions and 18 deletions

View file

@ -2655,7 +2655,7 @@ gameReturn_t idGameLocal::RunFrame(const usercmd_t* clientCmds) {
// see if a target_sessionCommand has forced a changelevel
if ( sessionCommand.Length() ) {
strncpy( ret.sessionCommand, sessionCommand, sizeof( ret.sessionCommand ) );
idStr::Copynz( ret.sessionCommand, sessionCommand, sizeof( ret.sessionCommand ) );
break;
}
@ -4852,7 +4852,7 @@ idGameLocal::GetBestGameType
*/
void idGameLocal::GetBestGameType( const char* map, const char* gametype, char buf[ MAX_STRING_CHARS ] ) {
idStr aux = mpGame.GetBestGametype( map, gametype );
strncpy( buf, aux.c_str(), MAX_STRING_CHARS );
idStr::Copynz( buf, aux.c_str(), MAX_STRING_CHARS );
buf[ MAX_STRING_CHARS - 1 ] = '\0';
}

View file

@ -1566,7 +1566,7 @@ gameReturn_t idGameLocal::ClientPrediction( int clientNum, const usercmd_t *clie
}
if ( sessionCommand.Length() ) {
strncpy( ret.sessionCommand, sessionCommand, sizeof( ret.sessionCommand ) );
idStr::Copynz( ret.sessionCommand, sessionCommand, sizeof( ret.sessionCommand ) );
}
return ret;
}

View file

@ -1461,7 +1461,7 @@ void idSessionLocal::LoadLoadingGui( const char *mapName ) {
stripped.StripPath();
char guiMap[ MAX_STRING_CHARS ];
strncpy( guiMap, va( "guis/map/%s.gui", stripped.c_str() ), MAX_STRING_CHARS );
idStr::Copynz( guiMap, va( "guis/map/%s.gui", stripped.c_str() ), MAX_STRING_CHARS );
// give the gamecode a chance to override
game->GetMapLoadingGUI( guiMap );

View file

@ -2403,7 +2403,7 @@ gameReturn_t idGameLocal::RunFrame( const usercmd_t *clientCmds ) {
// see if a target_sessionCommand has forced a changelevel
if ( sessionCommand.Length() ) {
strncpy( ret.sessionCommand, sessionCommand, sizeof( ret.sessionCommand ) );
idStr::Copynz( ret.sessionCommand, sessionCommand, sizeof( ret.sessionCommand ) );
break;
}
@ -4368,7 +4368,7 @@ idGameLocal::GetBestGameType
============
*/
void idGameLocal::GetBestGameType( const char* map, const char* gametype, char buf[ MAX_STRING_CHARS ] ) {
strncpy( buf, gametype, MAX_STRING_CHARS );
idStr::Copynz( buf, gametype, MAX_STRING_CHARS );
buf[ MAX_STRING_CHARS - 1 ] = '\0';
}

View file

@ -1521,7 +1521,7 @@ gameReturn_t idGameLocal::ClientPrediction( int clientNum, const usercmd_t *clie
}
if ( sessionCommand.Length() ) {
strncpy( ret.sessionCommand, sessionCommand, sizeof( ret.sessionCommand ) );
idStr::Copynz( ret.sessionCommand, sessionCommand, sizeof( ret.sessionCommand ) );
}
return ret;
}

View file

@ -1275,7 +1275,7 @@ const char *Mem_CleanupFileName( const char *fileName ) {
newFileName = newFileName.Left( i2 - 1 ) + newFileName.Right( newFileName.Length() - ( i1 + 4 ) );
}
index = ( index + 1 ) & 3;
strncpy( newFileNames[index], newFileName.c_str(), sizeof( newFileNames[index] ) );
idStr::Copynz( newFileNames[index], newFileName.c_str(), sizeof( newFileNames[index] ) );
return newFileNames[index];
}

View file

@ -277,7 +277,10 @@ void idRenderWorldLocal::WriteLoadMap() {
session->writeDemo->WriteInt( DC_LOADMAP );
demoHeader_t header;
// DG: Note: here strncpy() makes sense, because all chars of mapname get written
// so it's good if the ones behind the actual name are *all* \0
strncpy( header.mapname, mapName.c_str(), sizeof( header.mapname ) - 1 );
header.mapname[sizeof( header.mapname ) - 1] = '\0'; // make sure the last chars is also \0
header.version = 4;
header.sizeofRenderEntity = sizeof( renderEntity_t );
header.sizeofRenderLight = sizeof( renderLight_t );

View file

@ -630,7 +630,7 @@ int R_FindARBProgram( GLenum target, const char *program ) {
// add it to the list and load it
progs[i].ident = (program_t)0; // will be gen'd by R_LoadARBProgram
progs[i].target = target;
strncpy( progs[i].name, program, sizeof( progs[i].name ) - 1 );
idStr::Copynz( progs[i].name, program, sizeof( progs[i].name ) );
R_LoadARBProgram( i );

View file

@ -117,8 +117,7 @@ ExtractPort
*/
static bool ExtractPort( const char *src, char *buf, int bufsize, int *port ) {
char *p;
strncpy( buf, src, bufsize );
p = buf; p += Min( bufsize - 1, (int)strlen( src ) ); *p = '\0';
idStr::Copynz( buf, src, bufsize );
p = strchr( buf, ':' );
if ( !p ) {
return false;

View file

@ -170,5 +170,5 @@ Sys_SetFatalError
==================
*/
void Sys_SetFatalError( const char *error ) {
strncpy( fatalError, error, sizeof( fatalError ) );
idStr::Copynz( fatalError, error, sizeof( fatalError ) );
}

View file

@ -57,7 +57,7 @@ bool Sys_GetPath(sysPath_t type, idStr &path) {
switch(type) {
case PATH_BASE:
strncpy(buf, [ [ [ NSBundle mainBundle ] bundlePath ] cString ], MAXPATHLEN );
SDL_strlcpy(buf, [ [ [ NSBundle mainBundle ] bundlePath ] cString ], MAXPATHLEN );
snap = strrchr(buf, '/');
if (snap)
*snap = '\0';
@ -196,7 +196,7 @@ int SDL_main( int argc, char *argv[] ) {
Sys_Error("Could not access application resources");
// DG: set exe_path so Posix_InitSignalHandlers() can call Posix_GetExePath()
strncpy(exe_path, [ [ [ NSBundle mainBundle ] bundlePath ] cString ], MAXPATHLEN);
SDL_strlcpy(exe_path, [ [ [ NSBundle mainBundle ] bundlePath ] cString ], MAXPATHLEN);
Posix_InitSignalHandlers(); // DG: added signal handlers for POSIX platforms

View file

@ -109,8 +109,7 @@ ExtractPort
*/
static bool ExtractPort( const char *src, char *buf, int bufsize, int *port ) {
char *p;
strncpy( buf, src, bufsize );
p = buf; p += Min( bufsize - 1, (int)strlen( src ) ); *p = '\0';
idStr::Copynz( buf, src, bufsize );
p = strchr( buf, ':' );
if ( !p ) {
return false;

View file

@ -1178,6 +1178,7 @@ void idSysLocal::StartProcess( const char *exePath, bool doexit ) {
si.cb = sizeof(si);
strncpy( szPathOrig, exePath, _MAX_PATH );
szPathOrig[_MAX_PATH-1] = 0;
if( !CreateProcess( NULL, szPathOrig, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) ) {
common->Error( "Could not start process: '%s' ", szPathOrig );

View file

@ -163,8 +163,7 @@ Net_ExtractPort
*/
static bool Net_ExtractPort( const char *src, char *buf, int bufsize, int *port ) {
char *p;
strncpy( buf, src, bufsize );
p = buf; p += Min( bufsize - 1, (int)strlen( src ) ); *p = '\0';
idStr::Copynz( buf, src, bufsize );
p = strchr( buf, ':' );
if ( !p ) {
return false;