]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
fmt: promptly diagnose write errors
authorPádraig Brady <P@draigBrady.com>
Wed, 22 Oct 2025 15:11:53 +0000 (16:11 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 22 Oct 2025 15:18:06 +0000 (16:18 +0100)
* NEWS: Mention the improvement.
* src/fmt.c (put_line): Exit if any error writing line.
(flush_paragraph): Exit if any error writing buffer.
* tests/misc/write-errors.sh: Enable the (flush_paragraph) test case,
and add another to check the put_line() case.

NEWS
src/fmt.c
tests/misc/write-errors.sh

diff --git a/NEWS b/NEWS
index b49b73c547aa310200cb6ae9fd0bb3504588d7ea..55fe59503eb18ac30a36192279a0e5f5ed4bc404 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -43,6 +43,9 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** Improvements
 
+  'fmt' will now exit promptly upon receiving a write error,
+  which is significant when reading large / unbounded inputs.
+
   'install' now uses posix_spawn() to invoke the strip program more efficiently.
 
   'numfmt':
index 244a8a5c926c1f77d2d800d4bf96f906eb482300..7a02205a84cf75978406f4500370a61cb4b97e54 100644 (file)
--- a/src/fmt.c
+++ b/src/fmt.c
@@ -810,7 +810,10 @@ flush_paragraph (void)
 
   if (word_limit == word)
     {
-      fwrite (parabuf, sizeof *parabuf, wptr - parabuf, stdout);
+      size_t to_write = wptr - parabuf;
+      if (fwrite (parabuf, 1, to_write, stdout) != to_write)
+        write_error ();
+
       wptr = parabuf;
       return;
     }
@@ -1010,6 +1013,9 @@ put_line (WORD *w, int indent)
   put_word (w);
   last_line_length = out_column;
   putchar ('\n');
+
+  if (ferror (stdout))
+    write_error ();
 }
 
 /* Output to stdout the word W.  */
index 782ccae5a719bd7ff5618a79493096cfe4ffb712..48f9c0f1a8417378dbf767aaee00735dda03b7ab 100755 (executable)
@@ -33,7 +33,8 @@ cut -z -f1- /dev/zero
 dd if=/dev/zero
 expand /dev/zero
 factor --version; yes 1 | factor
-# TODO: fmt /dev/zero
+fmt /dev/zero
+fmt --version; yes | fmt
 fold /dev/zero
 fold -b /dev/zero
 fold -c /dev/zero