On 06/03/2024 15:32, Daniel Hofstetter wrote:
Hi,

I noticed some unexpected behavior and I'm unsure whether it's a bug
or a feature.

When using --untagged with non legacy checksums, the mode flag in the
output is ' ' (space), meaning text mode:

$ printf "hello" | cksum --untagged --algo=md5
5d41402abc4b2a76b9719d911017c592  -

However, if I add --tag, the mode flag changes to '*', meaning binary mode:

$ printf "hello" | cksum --tag --untagged --algo=md5
5d41402abc4b2a76b9719d911017c592 *-

According to 
https://www.gnu.org/software/coreutils/manual/html_node/cksum-common-options.html#index-_002d_002dtag,
--tag implies binary mode. On the other hand, --untagged overrides
--tag and so I expected text mode in the output.

So, is this behavior a bug or a feature?

I'm using coreutils 9.4 on Linux.

That's an edge case, but still a bug.
Should be fixed with the attached.

thanks,
Pádraig
From 7b92e9fb2643371ebcecc092d8f4453a7ff23a75 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <p...@draigbrady.com>
Date: Wed, 6 Mar 2024 21:54:02 +0000
Subject: [PATCH] cksum: ensure appropriate "binary" mode with --untagged

* src/digest.c (main): If --binary was enabled with a previous --tag,
then reset the binary mode to auto select if --untagged then specified.
* tests/cksum/cksum-a.sh: Add a test case.
---
 src/digest.c           | 19 ++++++++++++-------
 tests/cksum/cksum-a.sh |  9 +++++++++
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/digest.c b/src/digest.c
index 96b811b6c..1a4cfd1fb 100644
--- a/src/digest.c
+++ b/src/digest.c
@@ -28,6 +28,10 @@
 #include "xdectoint.h"
 #include "xstrtol.h"
 
+#ifndef HASH_ALGO_CKSUM
+# define HASH_ALGO_CKSUM 0
+#endif
+
 #if HASH_ALGO_SUM || HASH_ALGO_CKSUM
 # include "sum.h"
 #endif
@@ -1346,11 +1350,7 @@ main (int argc, char **argv)
   int opt;
   bool ok = true;
   int binary = -1;
-#if HASH_ALGO_CKSUM
-  bool prefix_tag = true;
-#else
-  bool prefix_tag = false;
-#endif
+  int prefix_tag = -1;
 
   /* Setting values of global variables.  */
   initialize_main (&argc, &argv);
@@ -1438,11 +1438,13 @@ main (int argc, char **argv)
         raw_digest = true;
         break;
       case UNTAG_OPTION:
-        prefix_tag = false;
+        if (prefix_tag == 1)
+          binary = -1;
+        prefix_tag = 0;
         break;
 # endif
       case TAG_OPTION:
-        prefix_tag = true;
+        prefix_tag = 1;
         binary = 1;
         break;
       case 'z':
@@ -1518,6 +1520,9 @@ main (int argc, char **argv)
    }
 #endif
 
+  if (prefix_tag == -1)
+    prefix_tag = HASH_ALGO_CKSUM;
+
   if (prefix_tag && !binary)
    {
      /* This could be supported in a backwards compatible way
diff --git a/tests/cksum/cksum-a.sh b/tests/cksum/cksum-a.sh
index f01e78a3a..2933940c6 100755
--- a/tests/cksum/cksum-a.sh
+++ b/tests/cksum/cksum-a.sh
@@ -58,4 +58,13 @@ returns_ 1 cksum -a bsd --check </dev/null || fail=1
 # Ensure abbreviations not supported for algorithm selection
 returns_ 1 cksum -a sha22 </dev/null || fail=1
 
+# Ensure --tag -> --untagged transition resets binary indicator
+cksum --tag --untagged -a md5 /dev/null >out-1 || fail=1
+# --binary ignored in this edge case
+cksum --binary --tag --untagged -a md5 /dev/null >out-2 || fail=1
+# base case for comparison
+cksum --untagged -a md5 /dev/null >out || fail=1
+compare out out-1 || fail=1
+compare out out-2 || fail=1
+
 Exit $fail
-- 
2.43.0

Reply via email to