fix incorrect use of strncpy

this function should really be removed or rewritten from scratch
This commit is contained in:
Ludwig Nussel 2007-10-08 10:26:28 +00:00
parent da8f451263
commit 95f67c2c3e

View file

@ -246,18 +246,17 @@ Sys_ANSIColorify
Transform Q3 colour codes to ANSI escape sequences Transform Q3 colour codes to ANSI escape sequences
================= =================
*/ */
static void Sys_ANSIColorify( const char *msg, char *buffer, int bufferSize ) //XXX: function should behave like others that colorize strings
static void Sys_ANSIColorify( const char *msg, char *buffer, unsigned bufferSize )
{ {
int msgLength, pos; int msgLength;
int i, j; int i, j;
char *escapeCode; char *escapeCode;
char tempBuffer[ 7 ];
if( !msg || !buffer ) if( !msg || !buffer )
return; return;
msgLength = strlen( msg ); msgLength = strlen( msg );
pos = 0;
i = 0; i = 0;
buffer[ 0 ] = '\0'; buffer[ 0 ] = '\0';
@ -265,8 +264,11 @@ static void Sys_ANSIColorify( const char *msg, char *buffer, int bufferSize )
{ {
if( msg[ i ] == '\n' ) if( msg[ i ] == '\n' )
{ {
Com_sprintf( tempBuffer, 7, "%c[0m\n", 0x1B ); unsigned len = 4;
strncat( buffer, tempBuffer, bufferSize ); if(bufferSize <= len) goto out;
strcpy( buffer, "\x1b[m\n");
buffer += len;
bufferSize -= len;
i++; i++;
} }
else if( msg[ i ] == Q_COLOR_ESCAPE ) else if( msg[ i ] == Q_COLOR_ESCAPE )
@ -276,6 +278,7 @@ static void Sys_ANSIColorify( const char *msg, char *buffer, int bufferSize )
if( i < msgLength ) if( i < msgLength )
{ {
escapeCode = NULL; escapeCode = NULL;
// XXX: no need for that loop
for( j = 0; j < CON_colorTableSize; j++ ) for( j = 0; j < CON_colorTableSize; j++ )
{ {
if( msg[ i ] == CON_colorTable[ j ].Q3color ) if( msg[ i ] == CON_colorTable[ j ].Q3color )
@ -287,8 +290,11 @@ static void Sys_ANSIColorify( const char *msg, char *buffer, int bufferSize )
if( escapeCode ) if( escapeCode )
{ {
Com_sprintf( tempBuffer, 7, "%c[%sm", 0x1B, escapeCode ); unsigned len = 3 + strlen(escapeCode);
strncat( buffer, tempBuffer, bufferSize ); if(bufferSize <= len) goto out;
Com_sprintf( buffer, len+1, "\x1b[%sm", escapeCode );
buffer += len;
bufferSize -= len;
} }
i++; i++;
@ -296,10 +302,14 @@ static void Sys_ANSIColorify( const char *msg, char *buffer, int bufferSize )
} }
else else
{ {
Com_sprintf( tempBuffer, 7, "%c", msg[ i++ ] ); if(bufferSize <= 1) goto out;
strncat( buffer, tempBuffer, bufferSize ); *buffer++ = msg[ i++ ];
*buffer = 0;
--bufferSize;
} }
} }
out:
return;
} }
/* /*