From: Rudolf Polzer <divver...@gmail.com>

Fixes endless loop when stdin ends:

```
$ src/gnuchess </dev/null
GNU Chess
White (1) : Can't open file "(null)": Bad address - using defaults
Invalid move:
White (1) : Invalid move:
White (1) : Invalid move:
White (1) : Invalid move:
White (1) : Invalid move:
White (1) : Invalid move:
White (1) : Invalid move:
[...]
```

and

```
$ src/gnuchess --uci </dev/null
GNU Chess
```

(no log spam, but process hangs and eats CPU)

Implementation is separate for UCI and XBoard/console protocols 

---
 src/frontend/common.h  |  2 +-
 src/frontend/engine.cc |  2 +-
 src/frontend/input.cc  | 13 ++++++++-----
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/frontend/common.h b/src/frontend/common.h
index 193bd62..b4e8141 100644
--- a/src/frontend/common.h
+++ b/src/frontend/common.h
@@ -515,7 +515,7 @@ void input_wakeup(void);
  * Input routine, initialized to one of the specific
  * input routines. The given argument is the prompt.
  */
-extern void (*get_line) (char *);
+extern int (*get_line) (char *);
 
 #define BUF_SIZE 4096
 
diff --git a/src/frontend/engine.cc b/src/frontend/engine.cc
index 6f8cf50..16a881f 100644
--- a/src/frontend/engine.cc
+++ b/src/frontend/engine.cc
@@ -523,7 +523,7 @@ void ForwardUserInputToEngine( void )
     assert( nread <= BUF_SIZE-2 );
     userinputaux[nread] = '\n';
     userinputaux[nread+1] = '\0';
-    if ( strcmp(userinputaux,"quit\n") == 0 || strcmp(userinputaux,"quit\n\n") 
== 0 ) {
+    if ( nread == 0 || strcmp(userinputaux,"quit\n") == 0 || 
strcmp(userinputaux,"quit\n\n") == 0 ) {
          SET (flags, QUIT);
     }
     int outError=0;
diff --git a/src/frontend/input.cc b/src/frontend/input.cc
index a8e4493..c2d90a9 100644
--- a/src/frontend/input.cc
+++ b/src/frontend/input.cc
@@ -54,7 +54,7 @@ extern char* readline(char *);
 extern void add_history(char *);
 # endif
 #endif
-void (*get_line)(char * p);
+int (*get_line)(char * p);
 
 /* Variable used to communicate with the main thread */
 volatile int input_status = INPUT_NONE;
@@ -71,7 +71,7 @@ pthread_mutex_t     input_mutex = PTHREAD_MUTEX_INITIALIZER;
  */
 
 #ifdef HAVE_LIBREADLINE
-void getline_readline(char * p)
+int getline_readline(char * p)
 {
   char *inp;
 
@@ -86,18 +86,19 @@ void getline_readline(char * p)
   if (inp) {
     free(inp);
   }
+  return inp != NULL;
 }
 #endif /* HAVE_LIBREADLINE */
 
 /* The generic input routine */
 
-void getline_standard(char *p)
+int getline_standard(char *p)
 {
   if (!(flags & XBOARD)) {
     fputs(p, stdout);
     fflush(stdout);
   }
-  if ( fgets(userinputstr, MAXSTR, stdin) );  // TODO Handle return value
+  return fgets(userinputstr, MAXSTR, stdin) != NULL;
 }
 
 /*
@@ -150,7 +151,9 @@ void *input_func(void *arg __attribute__((unused)) )
              RealSide ? _("Black") : _("White"), 
              (RealGameCnt+1)/2 + 1 );
     }
-    get_line(prompt);
+    if (!get_line(prompt)) {
+      SET(flags, QUIT);
+    }
     SendToFrontend( userinputstr );
 #ifdef HAVE_LIBREADLINE
     const char new_line[]="\n";
-- 
2.39.5


Reply via email to