Skip to content

Commit

Permalink
Allow src and dest buffers to be overlapped in Q_strncpy() for QVM co…
Browse files Browse the repository at this point in the history
…mpatibility reasons
  • Loading branch information
ec- committed Jun 7, 2024
1 parent 769b69a commit 68b1e58
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
44 changes: 38 additions & 6 deletions code/qcommon/q_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,24 +1309,56 @@ void Q_strncpyz( char *dest, const char *src, int destsize )
/*
=============
Q_strncpy
allows src and dest to be overlapped for QVM compatibility purposes
=============
*/
char *Q_strncpy( char *dest, const char *src, int destsize )
char *Q_strncpy( char *dest, char *src, int destsize )
{
char *start = dest;
char *s = src, *start = dest;
int src_len;

while ( destsize > 0 && (*dest++ = *src++) != '\0' ) {
--destsize;
while ( *s != '\0' )
++s;
src_len = (int)(s - src);

if ( src_len > destsize ) {
src_len = destsize;
}
destsize -= src_len;

if ( dest > src && dest < src + src_len ) {
int i;
#ifdef _DEBUG
Com_Printf( S_COLOR_YELLOW "Q_strncpy: overlapped (dest > src) buffers\n" );
#endif
for ( i = src_len - 1; i >= 0; --i ) {
dest[i] = src[i]; // back overlapping
}
dest += src_len;
} else {
#ifdef _DEBUG
if ( src >= dest && src < dest + src_len ) {
Com_Printf( S_COLOR_YELLOW "Q_strncpy: overlapped (src >= dst) buffers\n" );
#ifdef _MSC_VER
// __debugbreak();
#endif
}
#endif
while ( src_len > 0 ) {
*dest++ = *src++;
--src_len;
}
}

while ( --destsize > 0 ) {
while ( destsize > 0 ) {
*dest++ = '\0';
--destsize;
}

return start;
}


/*
=============
Q_stricmpn
Expand Down
2 changes: 1 addition & 1 deletion code/qcommon/q_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ void Q_strcat( char *dest, int size, const char *src );
int Q_replace( const char *str1, const char *str2, char *src, int max_len );

char *Q_stradd( char *dst, const char *src );
char *Q_strncpy( char *dest, const char *src, int destsize );
char *Q_strncpy( char *dest, char *src, int destsize );

// strlen that discounts Quake color sequences
int Q_PrintStrlen( const char *string );
Expand Down
2 changes: 1 addition & 1 deletion code/server/sv_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ static intptr_t SV_GameSystemCalls( intptr_t *args ) {
return 0;
case G_GET_ENTITY_TOKEN:
{
const char *s = COM_Parse( &sv.entityParsePoint );
char *s = (char*)COM_Parse( &sv.entityParsePoint );
VM_CHECKBOUNDS( gvm, args[1], args[2] );
//Q_strncpyz( VMA(1), s, args[2] );
// we can't use our optimized Q_strncpyz() function
Expand Down

0 comments on commit 68b1e58

Please sign in to comment.