From cf312a02801f825fb4ea7b20c02f78666cddcc72 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 3 Aug 2024 10:59:30 +0300 Subject: [PATCH] engine: fix buffer overflow in Sys_PrintLog --- engine/common/sys_con.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/engine/common/sys_con.c b/engine/common/sys_con.c index ffb6d3a827..bec6e08900 100644 --- a/engine/common/sys_con.c +++ b/engine/common/sys_con.c @@ -297,6 +297,7 @@ void Sys_PrintLog( const char *pMsg ) const struct tm *crt_tm; char logtime[32] = ""; static char lastchar; + size_t len; time( &crt_time ); crt_tm = localtime( &crt_time ); @@ -307,10 +308,12 @@ void Sys_PrintLog( const char *pMsg ) // spew to stdout Sys_PrintStdout( logtime, pMsg ); + len = Q_strlen( pMsg ); + if( !s_ld.logfile ) { // save last char to detect when line was not ended - lastchar = pMsg[Q_strlen( pMsg ) - 1]; + lastchar = len > 0 ? pMsg[len - 1] : 0; return; } @@ -318,7 +321,7 @@ void Sys_PrintLog( const char *pMsg ) strftime( logtime, sizeof( logtime ), "[%Y:%m:%d|%H:%M:%S] ", crt_tm ); //full time // save last char to detect when line was not ended - lastchar = pMsg[Q_strlen( pMsg ) - 1]; + lastchar = len > 0 ? pMsg[len - 1] : 0; Sys_PrintLogfile( s_ld.logfileno, logtime, pMsg, false ); Sys_FlushLogfile();