From a5191d8948f6d55f9c3bdd79b76a4fa2959d5722 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 8 Mar 2025 02:35:29 +0300 Subject: [PATCH] engine: client: console: simplify Field_DrawInputLine, try to make it safer --- engine/client/console.c | 47 ++++++++--------------------------------- 1 file changed, 9 insertions(+), 38 deletions(-) diff --git a/engine/client/console.c b/engine/client/console.c index 06b6503a..9a97b162 100644 --- a/engine/client/console.c +++ b/engine/client/console.c @@ -1267,45 +1267,16 @@ static void Field_CharEvent( field_t *edit, int ch ) Field_DrawInputLine ================== */ -static void Field_DrawInputLine( int x, int y, field_t *edit ) +static void Field_DrawInputLine( int x, int y, const field_t *edit ) { - int len, cursorChar; - int drawLen; - int prestep, curPos; - char str[MAX_SYSPATH]; - byte *colorDefault; + int curPos; + char str[MAX_SYSPATH]; + const byte *colorDefault = g_color_table[ColorIndex( COLOR_DEFAULT )]; + const int prestep = bound( 0, edit->scroll, sizeof( edit->buffer ) - 1 ); + const int drawLen = bound( 0, edit->widthInChars, sizeof( str )); + const int cursorCharPos = bound( 0, edit->cursor - prestep, sizeof( str )); - drawLen = edit->widthInChars; - len = Q_strlen( edit->buffer ) + 1; - colorDefault = g_color_table[ColorIndex( COLOR_DEFAULT )]; - - // guarantee that cursor will be visible - if( len <= drawLen ) - { - prestep = 0; - } - else - { - if( edit->scroll + drawLen > len ) - { - edit->scroll = len - drawLen; - if( edit->scroll < 0 ) edit->scroll = 0; - } - - prestep = edit->scroll; - } - - if( prestep + drawLen > len ) - drawLen = len - prestep; - - // extract characters from the field at - drawLen = Q_min( drawLen, MAX_SYSPATH - 1 ); - - memcpy( str, edit->buffer + prestep, drawLen ); - str[drawLen] = 0; - - // save char for overstrike - cursorChar = str[edit->cursor - prestep]; + Q_strncpy( str, edit->buffer + prestep, drawLen ); // draw it CL_DrawString( x, y, str, colorDefault, con.curFont, FONT_DRAW_UTF8 ); @@ -1314,7 +1285,7 @@ static void Field_DrawInputLine( int x, int y, field_t *edit ) if((int)( host.realtime * 4 ) & 1 ) return; // off blink // calc cursor position - str[edit->cursor - prestep] = 0; + str[cursorCharPos] = 0; CL_DrawStringLen( con.curFont, str, &curPos, NULL, FONT_DRAW_UTF8 ); if( host.key_overstrike )