]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
uniq: promptly diagnose write errors
authorPádraig Brady <P@draigBrady.com>
Sat, 15 Jul 2023 20:10:38 +0000 (21:10 +0100)
committerPádraig Brady <P@draigBrady.com>
Mon, 17 Jul 2023 10:28:36 +0000 (11:28 +0100)
* src/uniq.c (write_line): Check the output from fwrite() immediately.
(check_file): Likewise.
* tests/misc/write-errors.sh: Enable the test case.
* NEWS: Mention the improvement.

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

diff --git a/NEWS b/NEWS
index 892db27b8d0e3df4b7567e993b759ce132069bb1..dc5d21d9b508f26c0910a82780c06b2a694cbd86 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -62,7 +62,7 @@ GNU coreutils NEWS                                    -*- outline -*-
   irrespective of which kernel version coreutils is built against,
   reinstating that behaviour from coreutils-9.0.
 
-  od will now exit immediately upon receiving a write error, which is
+  od and uniq will now exit immediately upon receiving a write error, which is
   significant when reading large / unbounded inputs.
 
   split now uses more tuned access patterns for its potentially large input.
index 15611540b564ae96808be14ddfa472fca1f215b5..2dbec9fcdf21374aa8aa31acbc9366eecf6675b4 100644 (file)
@@ -309,7 +309,9 @@ writeline (struct linebuffer const *line,
   if (countmode == count_occurrences)
     printf ("%7" PRIuMAX " ", linecount + 1);
 
-  fwrite (line->buffer, sizeof (char), line->length, stdout);
+  if (fwrite (line->buffer, sizeof (char), line->length, stdout)
+      != line->length)
+    write_error ();
 }
 
 /* Process input file INFILE with output to OUTFILE.
@@ -378,8 +380,9 @@ check_file (char const *infile, char const *outfile, char delimiter)
 
           if (new_group || grouping != GM_NONE)
             {
-              fwrite (thisline->buffer, sizeof (char),
-                      thisline->length, stdout);
+              if (fwrite (thisline->buffer, sizeof (char), thisline->length,
+                  stdout) != thisline->length)
+                write_error ();
 
               SWAP_LINES (prevline, thisline);
               prevfield = thisfield;
index 31b6433d7bd6fc271614b9c417012249b7937b8b..20942e06856aa47104cbdc7a9e0b4706a4cbffcb 100755 (executable)
@@ -46,7 +46,7 @@ tail -n+1 -z /dev/zero
 tee < /dev/zero
 tr . . < /dev/zero
 unexpand /dev/zero
-# TODO: uniq -z -D /dev/zero
+uniq -z -D /dev/zero
 yes
 " |
 sort -k 1b,1 > all_writers || framework_failure_