From ca7711456f8cedbf40bec00ced04e7d6d9fc85be Mon Sep 17 00:00:00 2001 From: =?utf8?q?P=C3=A1draig=20Brady?=
Date: Sat, 15 Jul 2023 21:10:38 +0100 Subject: [PATCH] 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. --- NEWS | 2 +- src/uniq.c | 9 ++++++--- tests/misc/write-errors.sh | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 892db27b8d..dc5d21d9b5 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 15611540b5..2dbec9fcdf 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 31b6433d7b..20942e0685 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_ -- 2.47.2