[quake3-commits] r2139 - trunk/code/qcommon

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Fri Aug 5 17:45:22 EDT 2011


Author: ztm
Date: 2011-08-05 17:45:22 -0400 (Fri, 05 Aug 2011)
New Revision: 2139

Modified:
   trunk/code/qcommon/common.c
Log:
Simulate line buffering and fix the overflow bug in Com_ReadFromPipe(), patch from DevHC.

Modified: trunk/code/qcommon/common.c
===================================================================
--- trunk/code/qcommon/common.c	2011-08-05 20:47:02 UTC (rev 2138)
+++ trunk/code/qcommon/common.c	2011-08-05 21:45:22 UTC (rev 2139)
@@ -2883,15 +2883,45 @@
 */
 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;
+		}
+	}
 }
 
 



More information about the quake3-commits mailing list