git: c3f8900e6969 - main - uniq: Fix off-by-one bug in -cD case.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Mon, 09 Dec 2024 19:45:20 UTC
The branch main has been updated by des:

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

commit c3f8900e696998c410dc16f9bd9d45c24c413e6b
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-12-09 19:44:46 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-12-09 19:44:46 +0000

    uniq: Fix off-by-one bug in -cD case.
    
    When printing only duplicated lines, the first line of each set is not
    printed until we encounter the second.  When that happens, we need to
    increment the repetition count between printing the first and the
    second line, so that if we are also printing counts, we don't print the
    same (pre-increment) count twice.
    
    MFC after:      1 week
    PR:             275764
    Reported by:    Yu-Sheng Ma <s110062131@m110.nthu.edu.tw>
    Submitted by:   Daniel Tameling <tamelingdaniel@gmail.com> (original patch)
    Sponsored by:   Klara, Inc.
    Reviewed by:    tamelingdaniel_gmail.com, asomers, emaste
    Differential Revision:  https://reviews.freebsd.org/D48000
---
 usr.bin/uniq/tests/uniq_test.sh | 20 ++++++++++++++++++++
 usr.bin/uniq/uniq.c             |  3 ++-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/usr.bin/uniq/tests/uniq_test.sh b/usr.bin/uniq/tests/uniq_test.sh
index fa37c1959a77..fc143632bdd2 100755
--- a/usr.bin/uniq/tests/uniq_test.sh
+++ b/usr.bin/uniq/tests/uniq_test.sh
@@ -80,6 +80,25 @@ all_repeated_body() {
 	atf_check_uniq --all-repeated=separate
 }
 
+atf_test_case count_all_repeated
+count_all_repeated_head() {
+	atf_set descr "count and print every instance of repeated lines"
+}
+count_all_repeated_body() {
+	printf "a\na\nb\na\na\n" >input
+	printf "   1 a\n   2 a\n   1 a\n   2 a\n" >expected
+	atf_check_uniq -D -c
+	atf_check_uniq -Dnone -c
+	atf_check_uniq -cD
+	atf_check_uniq -cDnone
+	atf_check_uniq -c -D
+	atf_check_uniq -c -Dnone
+	atf_check_uniq --all-repeated --count
+	atf_check_uniq --all-repeated=none --count
+	atf_check_uniq --count --all-repeated
+	atf_check_uniq --count --all-repeated=none
+}
+
 atf_test_case skip_fields
 skip_fields_head() {
 	atf_set descr "skip fields"
@@ -196,6 +215,7 @@ atf_init_test_cases()
 	atf_add_test_case repeated
 	atf_add_test_case count_repeated
 	atf_add_test_case all_repeated
+	atf_add_test_case count_all_repeated
 	atf_add_test_case skip_fields
 	atf_add_test_case skip_fields_tab
 	atf_add_test_case ignore_case
diff --git a/usr.bin/uniq/uniq.c b/usr.bin/uniq/uniq.c
index 1c022e633cf3..0650ec2e2137 100644
--- a/usr.bin/uniq/uniq.c
+++ b/usr.bin/uniq/uniq.c
@@ -225,12 +225,13 @@ main (int argc, char *argv[])
 						fputc('\n', ofp);
 					show(ofp, prevline);
 				}
-				show(ofp, thisline);
 			} else if (dflag && !cflag) {
 				if (repeats == 0)
 					show(ofp, prevline);
 			}
 			++repeats;
+			if (Dflag)
+				show(ofp, thisline);
 		}
 	}
 	if (ferror(ifp))