Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Overlapping src and dest for strcpy in Client Code #639

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion code/client/cl_cgame.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) ) );
Expand Down
18 changes: 18 additions & 0 deletions code/client/cl_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion code/client/cl_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions code/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//
Expand Down