From: Simon McVittie <smcv@debian.org>
Date: Tue, 7 Mar 2023 11:46:00 +0000
Subject: sys_sdl_unix: Stop reading from stdin when EOF is reached

If the quakespasm server is run noninteractively with stdin redirected
from /dev/null (for example as a systemd service), this loop would
previously ignore EOF (read() returns 0) and append the uninitialized
contents of `c` to the buffer once per iteration, until the buffer is
full, at which point it would log "Console input too long!" and repeat.

For completeness, treat read errors (read() returns -1) as equivalent
to EOF.

Bug: https://sourceforge.net/p/quakespasm/bugs/59/
Bug-Debian: https://bugs.debian.org/1032276
Signed-off-by: Simon McVittie <smcv@debian.org>
---
 Quake/sys_sdl_unix.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/Quake/sys_sdl_unix.c b/Quake/sys_sdl_unix.c
index 8820dc5..061e946 100644
--- a/Quake/sys_sdl_unix.c
+++ b/Quake/sys_sdl_unix.c
@@ -417,20 +417,32 @@ double Sys_DoubleTime (void)
 
 const char *Sys_ConsoleInput (void)
 {
+	static qboolean	con_eof = false;
 	static char	con_text[256];
 	static int	textlen;
 	char		c;
 	fd_set		set;
 	struct timeval	timeout;
+	ssize_t n;
 
 	FD_ZERO (&set);
 	FD_SET (0, &set);	// stdin
 	timeout.tv_sec = 0;
 	timeout.tv_usec = 0;
 
+	if (con_eof)
+		return NULL;
+
 	while (select (1, &set, NULL, NULL, &timeout))
 	{
-		read (0, &c, 1);
+		n = read (0, &c, 1);
+		if (n <= 0)
+		{
+			// Finish processing whatever is already in the
+			// buffer (if anything), then stop reading
+			con_eof = true;
+			c = '\n';
+		}
 		if (c == '\n' || c == '\r')
 		{
 			con_text[textlen] = '\0';
