Fix Overlapping src and dest for strcpy in Client Code

This commit is contained in:
Bilgin COSKUN 2024-02-13 09:56:57 +03:00
parent 5d60f6035a
commit a1207f1637
4 changed files with 22 additions and 2 deletions

View File

@ -618,7 +618,7 @@ intptr_t CL_CgameSystemCalls( intptr_t *args ) {
Com_Memcpy( VMA(1), VMA(2), args[3] );
return 0;
case CG_STRNCPY:
strncpy( VMA(1), VMA(2), args[3] );
CL_Strncpy( VMA(1), VMA(2), args[3] );
return args[1];
case CG_SIN:
return FloatAsInt( sin( VMF(1) ) );

View File

@ -4708,3 +4708,21 @@ qboolean CL_CDKeyValidate( const char *key, const char *checksum ) {
return qfalse;
#endif
}
/*
=================
CL_Strncpy
=================
Safe reimplementation of strncpy against overlapping src & dest
*/
char *CL_Strncpy(char *dest, const char *src, unsigned long n){
unsigned long length = strlen(src);
if(n > length){
memmove(dest, src, length);
memset(dest+length,0,n-length);
}else{
memmove(dest, src, n);
}
return dest;
}

View File

@ -1005,7 +1005,7 @@ intptr_t CL_UISystemCalls( intptr_t *args ) {
return 0;
case UI_STRNCPY:
strncpy( VMA(1), VMA(2), args[3] );
CL_Strncpy( VMA(1), VMA(2), args[3] );
return args[1];
case UI_SIN:

View File

@ -485,6 +485,8 @@ void CL_InitRef( void );
qboolean CL_CDKeyValidate( const char *key, const char *checksum );
int CL_ServerStatus( char *serverAddress, char *serverStatusString, int maxLen );
char *CL_Strncpy(char *dest, const char *src, unsigned long n);
qboolean CL_CheckPaused(void);
//