From fea833591ba787b1232d13ac4b985bea1e7601de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
Date: Mon, 4 Mar 2024 16:33:23 +0000
Subject: [PATCH] cksum: consistently validate --length attributes

* src/digest.c (main): Only validate the last used --length
for being a multiple of 8.
* tests/cksum/b2sum.sh: Add a test case.
Fixes https://bugs.gnu.org/69546

Reference:https://github.com/coreutils/coreutils/commit/fea833591ba787b1232d13ac4b985bea1e7601de
Conflict:Adapt to src/digest.c. Adapt to tests/misc/b2sum.sh.

---
 src/digest.c        | 10 +++++-----
 tests/misc/b2sum.sh |  4 ++++
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/digest.c b/src/digest.c
index 2dfc877..84e2a6b 100644
--- a/src/digest.c
+++ b/src/digest.c
@@ -1299,11 +1299,6 @@ main (int argc, char **argv)
         digest_length = xdectoumax (optarg, 0, UINTMAX_MAX, "",
                                 _("invalid length"), 0);
         digest_length_str = optarg;
-        if (digest_length % 8 != 0)
-          {
-            error (0, 0, _("invalid length: %s"), quote (digest_length_str));
-            die (EXIT_FAILURE, 0, _("length is not a multiple of 8"));
-          }
         break;
 #endif
 #if !HASH_ALGO_SUM
@@ -1374,6 +1369,11 @@ main (int argc, char **argv)
     die (EXIT_FAILURE, 0,
          _("--length is only supported with --algorithm=blake2b"));
 # endif
+  if (digest_length % 8 != 0)
+    {
+      error (0, 0, _("invalid length: %s"), quote (digest_length_str));
+      error (EXIT_FAILURE, 0, _("length is not a multiple of 8"));
+    }
   if (digest_length > BLAKE2B_MAX_LEN * 8)
     {
       error (0, 0, _("invalid length: %s"), quote (digest_length_str));
diff --git a/tests/misc/b2sum.sh b/tests/misc/b2sum.sh
index 99982ca..3b02ecd 100755
--- a/tests/misc/b2sum.sh
+++ b/tests/misc/b2sum.sh
@@ -59,6 +59,10 @@ printf '%s\n' 'BLAKE2' 'BLAKE2b' 'BLAKE2-' 'BLAKE2(' 'BLAKE2 (' > crash.check \
   || framework_failure_
 returns_ 1 $prog -c crash.check || fail=1
 
+# This would fail before coreutil-9.4
+# Only validate the last specified, used length
+$prog -l 123 -l 128 /dev/null || fail=1
+
 done
 
 Exit $fail
-- 
2.33.0

