diff --git a/src/qcommon/common.c b/src/qcommon/common.c index 3613bcd5..5bc0f8d8 100644 --- a/src/qcommon/common.c +++ b/src/qcommon/common.c @@ -198,8 +198,7 @@ to the appropriate place. A raw string should NEVER be passed as fmt, because of "%f" type crashers. ============= */ -static qboolean devPrint = qfalse; -int FORMAT_PRINTF(1, 0) QDECL Com_VPrintf( const char *fmt, va_list argptr ) { +static int FORMAT_PRINTF(2, 0) QDECL Com_VPrintf( qboolean devPrint, const char *fmt, va_list argptr ) { static qboolean opening_qconsole = qfalse; char msg[MAXPRINTMSG]; int len; @@ -291,7 +290,7 @@ void FORMAT_PRINTF(1, 2) QDECL Com_Printf( const char *fmt, ... ) { va_list argptr; va_start( argptr, fmt ); - Com_VPrintf( fmt, argptr ); + Com_VPrintf( qfalse, fmt, argptr ); va_end( argptr ); } @@ -311,9 +310,7 @@ void FORMAT_PRINTF(1, 2) QDECL Com_DPrintf( const char *fmt, ...) { } va_start( argptr, fmt ); - devPrint = qtrue; - Com_VPrintf( fmt, argptr ); - devPrint = qfalse; + Com_VPrintf( qtrue, fmt, argptr ); va_end( argptr ); } diff --git a/src/qcommon/q_shared.c b/src/qcommon/q_shared.c index c958868c..86bf88e2 100644 --- a/src/qcommon/q_shared.c +++ b/src/qcommon/q_shared.c @@ -1533,18 +1533,51 @@ 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; - while ( --destsize > 0 ) { + 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 ) { *dest++ = '\0'; + --destsize; } return start; diff --git a/src/qcommon/qcommon.h b/src/qcommon/qcommon.h index 5b3d2726..939d2b1a 100644 --- a/src/qcommon/qcommon.h +++ b/src/qcommon/qcommon.h @@ -1097,7 +1097,6 @@ void Info_Print( const char *s ); void Com_BeginRedirect (char *buffer, int buffersize, void (*flush)(const char *)); void Com_EndRedirect( void ); -int QDECL Com_VPrintf( const char *fmt, va_list argptr ) FORMAT_PRINTF(1, 0); // conforms to vprintf prototype for print callback passing void QDECL Com_Printf( const char *fmt, ... ) FORMAT_PRINTF(1, 2); void QDECL Com_DPrintf( const char *fmt, ... ) FORMAT_PRINTF(1, 2); void NORETURN QDECL Com_Error( errorParm_t code, const char *fmt, ... ) FORMAT_PRINTF(2, 3);