Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 12 Jan 2024 15:44:31 GMT
From:      Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= <des@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 899837e8f574 - main - uniq: Error out if writing to the output failed.
Message-ID:  <202401121544.40CFiVAQ076744@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=899837e8f5741f9a847b63d9e7c8b76ccc033ab5

commit 899837e8f5741f9a847b63d9e7c8b76ccc033ab5
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-01-12 15:40:48 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-01-12 15:44:06 +0000

    uniq: Error out if writing to the output failed.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans
    Differential Revision:  https://reviews.freebsd.org/D43407
---
 usr.bin/uniq/tests/uniq_test.sh | 15 +++++++++++++++
 usr.bin/uniq/uniq.c             |  7 +++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/usr.bin/uniq/tests/uniq_test.sh b/usr.bin/uniq/tests/uniq_test.sh
index 537962eb6513..804e82ce7766 100755
--- a/usr.bin/uniq/tests/uniq_test.sh
+++ b/usr.bin/uniq/tests/uniq_test.sh
@@ -160,6 +160,20 @@ interactive_repeated_body() {
 	atf_check -o inline:"y\n" cat actual
 }
 
+atf_test_case stdout
+stdout_head() {
+	atf_set descr "error writing to stdout"
+}
+stdout_body() {
+	(
+		trap "" PIPE
+		echo a | uniq 2>stderr
+		echo $? >result
+	) | true
+	atf_check -o inline:"1\n" cat result
+	atf_check -o match:"stdout" cat stderr
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case basic
@@ -175,4 +189,5 @@ atf_init_test_cases()
 	atf_add_test_case count_unique
 	atf_add_test_case interactive
 	atf_add_test_case interactive_repeated
+	atf_add_test_case stdout
 }
diff --git a/usr.bin/uniq/uniq.c b/usr.bin/uniq/uniq.c
index ef59d7339d0c..9978c4996dc7 100644
--- a/usr.bin/uniq/uniq.c
+++ b/usr.bin/uniq/uniq.c
@@ -84,7 +84,7 @@ main (int argc, char *argv[])
 	int ch, comp;
 	size_t prevbuflen, thisbuflen, b1;
 	char *prevline, *thisline, *p;
-	const char *errstr, *ifn;
+	const char *errstr, *ifn, *ofn;
 	cap_rights_t rights;
 
 	(void) setlocale(LC_ALL, "");
@@ -142,6 +142,7 @@ main (int argc, char *argv[])
 	ifp = stdin;
 	ifn = "stdin";
 	ofp = stdout;
+	ofn = "stdout";
 	if (argc > 0 && strcmp(argv[0], "-") != 0)
 		ifp = file(ifn = argv[0], "r");
 	cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
@@ -149,7 +150,7 @@ main (int argc, char *argv[])
 		err(1, "unable to limit rights for %s", ifn);
 	cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
 	if (argc > 1)
-		ofp = file(argv[1], "w");
+		ofp = file(ofn = argv[1], "w");
 	else
 		cap_rights_set(&rights, CAP_IOCTL);
 	if (caph_rights_limit(fileno(ofp), &rights) < 0) {
@@ -240,6 +241,8 @@ main (int argc, char *argv[])
 	    (!dflag || (cflag && repeats > 0)) &&
 	    (!uflag || repeats == 0))
 		show(ofp, prevline);
+	if (fflush(ofp) != 0)
+		err(1, "%s", ofn);
 	exit(0);
 }
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202401121544.40CFiVAQ076744>