From a1207f1637662ab4c1f18f4d5ddec6060064f9fd Mon Sep 17 00:00:00 2001 From: Bilgin COSKUN Date: Tue, 13 Feb 2024 09:56:57 +0300 Subject: [PATCH] Fix Overlapping src and dest for strcpy in Client Code --- code/client/cl_cgame.c | 2 +- code/client/cl_main.c | 18 ++++++++++++++++++ code/client/cl_ui.c | 2 +- code/client/client.h | 2 ++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/code/client/cl_cgame.c b/code/client/cl_cgame.c index 613d084209..28eaeeae71 100644 --- a/code/client/cl_cgame.c +++ b/code/client/cl_cgame.c @@ -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) ) ); diff --git a/code/client/cl_main.c b/code/client/cl_main.c index 6c2579525c..4aa2809914 100644 --- a/code/client/cl_main.c +++ b/code/client/cl_main.c @@ -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; +} diff --git a/code/client/cl_ui.c b/code/client/cl_ui.c index 714364847f..dc927b52fd 100644 --- a/code/client/cl_ui.c +++ b/code/client/cl_ui.c @@ -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: diff --git a/code/client/client.h b/code/client/client.h index 0d3e1c3369..619346addc 100644 --- a/code/client/client.h +++ b/code/client/client.h @@ -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); //