From 708ae170c987dab83273cb885496e1a8a90233e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
Date: Sat, 27 Aug 2022 18:40:14 +0100
Subject: [PATCH] comm: fix NUL --output-delimiter with --total

* src/comm.c (compare_files): Handle the single character
--output-delimeter case separately so that NUL is appropriately
handled.
* doc/coreutils.texi (comm invocation): Fix the description
of --output-delimiter to say an empty delimeter is treated
as a NUL separator, rather than being disallowed.
* tests/misc/comm.pl: Add a test case.
Reported at https://bugs.debian.org/1014008

Reference:https://github.com/coreutils/coreutils/commit/708ae170c987dab83273cb885496e1a8a90233e8
Conflict:Context adaptation

---
 doc/coreutils.texi |  3 ++-
 src/comm.c         | 21 ++++++++++++++++-----
 tests/misc/comm.pl |  3 +++
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 9f31f6768..de819b6dc 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -5427,7 +5427,8 @@ Other options are:
 Print @var{str} between adjacent output columns,
 rather than the default of a single TAB character.
 
-The delimiter @var{str} may not be empty.
+The delimiter @var{str} may be empty, in which case
+the ASCII NUL character is used to delimit output columns.
 
 @item --total
 Output a summary at the end.
diff --git a/src/comm.c b/src/comm.c
index 721139cb8..ed9d97d0a 100644
--- a/src/comm.c
+++ b/src/comm.c
@@ -395,11 +395,22 @@ compare_files (char **infiles)
       char buf1[INT_BUFSIZE_BOUND (uintmax_t)];
       char buf2[INT_BUFSIZE_BOUND (uintmax_t)];
       char buf3[INT_BUFSIZE_BOUND (uintmax_t)];
-      printf ("%s%s%s%s%s%s%s%c",
-              umaxtostr (total[0], buf1), col_sep,
-              umaxtostr (total[1], buf2), col_sep,
-              umaxtostr (total[2], buf3), col_sep,
-              _("total"), delim);
+      if (col_sep_len == 1)
+        { /* Separate to handle NUL char.  */
+          printf ("%s%c%s%c%s%c%s%c",
+                  umaxtostr (total[0], buf1), *col_sep,
+                  umaxtostr (total[1], buf2), *col_sep,
+                  umaxtostr (total[2], buf3), *col_sep,
+                  _("total"), delim);
+        }
+      else
+        {
+          printf ("%s%s%s%s%s%s%s%c",
+                  umaxtostr (total[0], buf1), col_sep,
+                  umaxtostr (total[1], buf2), col_sep,
+                  umaxtostr (total[2], buf3), col_sep,
+                  _("total"), delim);
+        }
     }
 }
 
diff --git a/tests/misc/comm.pl b/tests/misc/comm.pl
index 73e8c3720..5d0c4f175 100755
--- a/tests/misc/comm.pl
+++ b/tests/misc/comm.pl
@@ -157,6 +157,9 @@ my @Tests =
     {OUT=>"1\n\0002\n\0002\n\000\0003\n\000\0003\n\000\0003\n"} ],
    ['zdelim-empty', '-z', '-z --output-delimiter=', @zinputs,
     {OUT=>"1\000\0002\000\0002\000\000\0003\000\000\0003\000\000\0003\000"} ],
+   ['total-delim-empty', '--total --output-delimiter=', @inputs,
+    {OUT=>"1\n\0002\n\0002\n\000\0003\n\000\0003\n\000\0003\n"
+        . "1\0002\0003\000total\n"} ],
 
    # invalid dual delimiter
    ['delim-dual', '--output-delimiter=,', '--output-delimiter=+', @inputs,
-- 
2.27.0

