diff options
author | Zack Middleton <zturtleman@gmail.com> | 2011-08-05 21:45:22 +0000 |
---|---|---|
committer | Tim Angus <tim@ngus.net> | 2013-01-10 23:26:58 +0000 |
commit | 6a2c93fd6d504d69015415ae9e3a6b1d5d36b372 (patch) | |
tree | 61eef56de249308cbf709dae77484041ecf1458d | |
parent | 0747aa2fdafbface0bf4db342f164b575b10bc29 (diff) |
Simulate line buffering and fix the overflow bug in Com_ReadFromPipe(), patch from DevHC.
-rw-r--r-- | src/qcommon/common.c | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/src/qcommon/common.c b/src/qcommon/common.c index 17ae4ee1..e8834d28 100644 --- a/src/qcommon/common.c +++ b/src/qcommon/common.c @@ -2737,15 +2737,45 @@ Read whatever is in com_pipefile, if anything, and execute it */ void Com_ReadFromPipe( void ) { - char buffer[MAX_STRING_CHARS] = {""}; - qboolean read; + static char buf[MAX_STRING_CHARS]; + static int accu = 0; + int read; if( !pipefile ) return; - read = FS_Read( buffer, sizeof( buffer ), pipefile ); - if( read ) - Cbuf_ExecuteText( EXEC_APPEND, buffer ); + while( ( read = FS_Read( buf + accu, sizeof( buf ) - accu - 1, pipefile ) ) > 0 ) + { + char *brk = NULL; + int i; + + for( i = accu; i < accu + read; ++i ) + { + if( buf[ i ] == '\0' ) + buf[ i ] = '\n'; + if( buf[ i ] == '\n' || buf[ i ] == '\r' ) + brk = &buf[ i + 1 ]; + } + buf[ accu + read ] = '\0'; + + accu += read; + + if( brk ) + { + char tmp = *brk; + *brk = '\0'; + Cbuf_ExecuteText( EXEC_APPEND, buf ); + *brk = tmp; + + accu -= brk - buf; + memmove( buf, brk, accu + 1 ); + } + else if( accu >= sizeof( buf ) - 1 ) // full + { + Cbuf_ExecuteText( EXEC_APPEND, buf ); + accu = 0; + } + } } |