]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
numfmt: promptly diagnose write errors
authorPádraig Brady <P@draigBrady.com>
Wed, 22 Oct 2025 12:53:46 +0000 (13:53 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 22 Oct 2025 14:53:06 +0000 (15:53 +0100)
* src/numfmt.c (process line): Inspect the stdio error state when
outputting each line so that we don't have to check each output function
but do eventually exit upon write error, while also remaining buffered.
(main): Also check when outputting a header for the edge case
of very long headers.
* tests/misc/write-errors.sh: Enable the numfmt test case.
* NEWS: Mention the improvement, and reorganize all numfmt improvements.

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

diff --git a/NEWS b/NEWS
index 7641e4f6ac7680409c079c682664024ab8762660..b49b73c547aa310200cb6ae9fd0bb3504588d7ea 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -45,9 +45,11 @@ GNU coreutils NEWS                                    -*- outline -*-
 
   'install' now uses posix_spawn() to invoke the strip program more efficiently.
 
-  numfmt now parses numbers with a non-breaking space character before a unit,
-  and parses numbers containing grouping characters from the current locale.
-  It also supports a multi-byte --delimiter character.
+  'numfmt':
+   - parses numbers with a non-breaking space character before a unit
+   - parses numbers containing grouping characters from the current locale
+   - supports a multi-byte --delimiter character
+   - no longer processes input indefinitely in the presence of write errors
 
   wc -l now operates 10% faster on hosts that support AVX512 instructions.
 
index 5a1e94570df07c4922559b2114aef514f04565b0..1ae831ba3f5d6ee6231db398a36c029ccf7b0526 100644 (file)
@@ -1512,6 +1512,9 @@ process_line (char *line, bool newline)
   if (newline)
     putchar (line_delim);
 
+ if (ferror (stdout))
+   write_error ();
+
   return valid_number;
 }
 
@@ -1710,7 +1713,10 @@ main (int argc, char **argv)
 
       while (header-- && getdelim (&line, &line_allocated,
                                    line_delim, stdin) > 0)
-        fputs (line, stdout);
+        {
+          if (fputs (line, stdout) == EOF)
+            write_error ();
+        }
 
       while ((len = getdelim (&line, &line_allocated,
                               line_delim, stdin)) > 0)
index b30f4f6bd38aed0a4026123af890a0d0cacd31b9..782ccae5a719bd7ff5618a79493096cfe4ffb712 100755 (executable)
@@ -41,7 +41,7 @@ fold --version; yes | tr -d '\\n' | fold
 head -z -n-1 /dev/zero
 join -a 1 -z /dev/zero /dev/null
 # TODO: nl --version; yes | nl
-# TODO: numfmt --version; yes 1 | numfmt
+numfmt --version; yes 1 | numfmt
 od -v /dev/zero
 paste /dev/zero
 # TODO: pr /dev/zero