From a610cf8231a02163a4a2b2faf3047d24798fe180 Mon Sep 17 00:00:00 2001
From: Tim Hallmann <tim@t8w.de>
Date: Sun, 24 Mar 2024 20:14:30 +0100
Subject: [PATCH] rev: Check for wchar conversion errors

Commit c9cc84621ca98ef85499e83ca56f05f12055f193 introduced a regression
where only the actual EOF is handled, not other error conditions
returning WEOF. This leads to an infinite loop upon encountering
conversion errors. For example (using LC_CTYPE="en_US.UTF-8"):

$ printf '\x80' | rev

Signed-off-by: Tim Hallmann <tim@t8w.de>

Reference:https://github.com/util-linux/util-linux/commit/a610cf8231a02163a4a2b2faf3047d24798fe180
Conflict:NA
---
 text-utils/rev.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/text-utils/rev.c b/text-utils/rev.c
index 81331719..4b731890 100644
--- a/text-utils/rev.c
+++ b/text-utils/rev.c
@@ -173,8 +173,6 @@ int main(int argc, char *argv[])
 		line = 0;
 		while (!feof(fp)) {
 			len = read_line(sep, buf, bufsiz, fp);
-			if (len == 0)
-				continue;
 
 			/* This is my hack from setpwnam.c -janl */
 			while (len == bufsiz && !feof(fp)) {
@@ -187,14 +185,18 @@ int main(int argc, char *argv[])
 				/* And fill the rest of the buffer */
 				len += read_line(sep, &buf[len], bufsiz/2, fp);
 			}
+			if (ferror(fp)) {
+				warn("%s: %ju", filename, line);
+				rval = EXIT_FAILURE;
+				break;
+			}
+			if (len == 0)
+				continue;
+
 			reverse_str(buf, buf[len - 1] == sep ? len - 1 : len);
 			write_line(buf, len, stdout);
 			line++;
 		}
-		if (ferror(fp)) {
-			warn("%s: %ju", filename, line);
-			rval = EXIT_FAILURE;
-		}
 		if (fp != stdin)
 			fclose(fp);
 	} while(*argv);
-- 
2.33.0

