[PATCH 2/4] alfred: avoid endless-loop when reading data from stdin fails

Sven Eckelmann <[email protected]> Fri, 31 Jul 2026 09:40:14 +0200
Newsgroups org.open-mesh.lists.batman
Message-ID <[email protected]>
alfred_client_set_data() read the dataset from stdin in a loop guarded only
by feof(). fread() returns 0 not only at end of file but also on a read
error. A persistent read error therefore made 'alfred -s' spin forever
without making progress.

Stop reading as soon as fread() cannot deliver any data, which covers both
end of file and read errors.

Fixes: 23359bbf72aa ("alfred: initial commit")
Signed-off-by: Sven Eckelmann <[email protected]>
---
 client.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/client.c b/client.c
index 9fa9f00..1967f69 100644
--- a/client.c
+++ b/client.c
@@ -153,6 +153,17 @@ int alfred_client_set_data(struct globals *globals)
 	len = sizeof(*push) + sizeof(*data);
 	while (!feof(stdin)) {
 		ret = fread(&buf[len], 1, sizeof(buf) - len, stdin);
+		if (ret == 0) {
+			if (ferror(stdin)) {
+				fprintf(stderr, "%s: failed to read data from stdin\n",
+					__func__);
+				unix_sock_close(globals);
+				return -1;
+			}
+
+			break;
+		}
+
 		len += ret;
 
 		if (sizeof(buf) == len)

-- 
2.47.3