aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2023-07-15 21:10:38 +0100
committerPádraig Brady <P@draigBrady.com>2023-07-17 11:28:36 +0100
commitca7711456f8cedbf40bec00ced04e7d6d9fc85be (patch)
tree835c2cfab8a43238f2d9b0c591db3936a826d077
parentod: promptly diagnose write errors (diff)
downloadcoreutils-ca7711456f8cedbf40bec00ced04e7d6d9fc85be.tar.gz
coreutils-ca7711456f8cedbf40bec00ced04e7d6d9fc85be.zip
uniq: promptly diagnose write errors
* 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.
-rw-r--r--NEWS2
-rw-r--r--src/uniq.c9
-rwxr-xr-xtests/misc/write-errors.sh2
3 files changed, 8 insertions, 5 deletions
diff --git a/NEWS b/NEWS
index 892db27b8..dc5d21d9b 100644
--- 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.
diff --git a/src/uniq.c b/src/uniq.c
index 15611540b..2dbec9fcd 100644
--- a/src/uniq.c
+++ b/src/uniq.c
@@ -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;
diff --git a/tests/misc/write-errors.sh b/tests/misc/write-errors.sh
index 31b6433d7..20942e068 100755
--- a/tests/misc/write-errors.sh
+++ b/tests/misc/write-errors.sh
@@ -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_