]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rev: Check for wchar conversion errors
authorTim Hallmann <tim@t8w.de>
Sun, 24 Mar 2024 19:14:30 +0000 (20:14 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 25 Mar 2024 18:40:46 +0000 (19:40 +0100)
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>
text-utils/rev.c

index 81331719d7725fa8481e4a3cf171b506455fdb9e..4b731890d8da732fd32e4bd348ec28b132327099 100644 (file)
@@ -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);