From 7b92e9fb2643371ebcecc092d8f4453a7ff23a75 Mon Sep 17 00:00:00 2001 From: =?utf8?q?P=C3=A1draig=20Brady?= 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 96b811b6c8..1a4cfd1fbf 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 f01e78a3a7..2933940c6b 100755 --- a/tests/cksum/cksum-a.sh +++ b/tests/cksum/cksum-a.sh @@ -58,4 +58,13 @@ returns_ 1 cksum -a bsd --check --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.47.2