[PATCH 4/4] Fix ForwardEngineOutputToUser overflow behavior.
[email protected] Mon, 2 Jun 2025 09:29:23 -0400
| Newsgroups | gmane.comp.gnu.chess.bugs |
|---|---|
| Message-ID | <[email protected]> |
From: Rudolf Polzer <[email protected]> The previous implementation read 4097 bytes, but always cleared the 4097th byte - so whenever the engine had more than 4096 bytes to send, one byte will get spuriously zeroed out, leading to lichess-bot complaining about `inf\x00` being an undefined UCI command and similar things. In theory this can even lead to the engine timing out and losing a game as the UCI command with the actual move has been corrupted, but I have never observed that happen. As here all data corruption happens within the buffer, this cannot be reproduced with AddressSanitizer. --- src/frontend/engine.cc | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/frontend/engine.cc b/src/frontend/engine.cc index e8fb3a2..cb7d974 100644 --- a/src/frontend/engine.cc +++ b/src/frontend/engine.cc @@ -551,7 +551,7 @@ void ForwardEngineOutputToUser( void ) fd_set set[1]; struct timeval time_val[1]; int engineinputready=0; - char engineinputaux[BUF_SIZE+1]=""; + char engineinputaux[BUF_SIZE]=""; /* Poll input from engine in non-blocking mode */ FD_ZERO(set); @@ -564,15 +564,13 @@ void ForwardEngineOutputToUser( void ) printf( "Error reading engine input.\n" ); } else if ( engineinputready > 0 ) { /* There are some data from the engine. Read the data */ - strncpy( engineinputaux, zerochar, BUF_SIZE+1 ); - nread = read( pipefd_e2a[0], engineinputaux, BUF_SIZE+1 ); - /* Write data to output */ - assert( nread <= BUF_SIZE+1 ); - if (nread < BUF_SIZE+1) { - engineinputaux[nread] = '\0'; - } else { - engineinputaux[BUF_SIZE] = '\0'; + nread = read( pipefd_e2a[0], engineinputaux, BUF_SIZE ); + if ( nread == -1 ) { + printf( "Error reading message from engine.\n" ); + return; } + /* Write data to output */ + assert( nread <= BUF_SIZE ); ssize_t r = write( STDOUT_FILENO, engineinputaux, nread ); if ( r == -1 ) { printf( "Error sending message to engine.\n" ); -- 2.39.5