]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cksum: use more exact selection of digest algorithms
authorPádraig Brady <P@draigBrady.com>
Sun, 30 Jan 2022 20:19:48 +0000 (20:19 +0000)
committerPádraig Brady <P@draigBrady.com>
Sun, 30 Jan 2022 23:13:52 +0000 (23:13 +0000)
Use more constrained argument matching
to improve forward compatibility and robustness.

For example it's better that `cksum -a sha3` is _not_
equivalent to `cksum -a sha386`, so that a user
specifying `-a sha3` on an older cksum would not be surprised.

Also argmatch() is used when parsing tags from lines like:
SHA3 (filename) = abcedf....
so it's more robust that older cksum instances to fail
earlier in the parsing process, when parsing output from
possible future cksum implementations that might support SHA3.

* src/digest.c (algorithm_from_tag): Use argmatch_exact()
to ensure we don't match abbreviated algorithms.
(main): Likewise.
* tests/misc/cksum-a.sh: Add a test case.

src/digest.c
tests/misc/cksum-a.sh

index 6de30ecb56b30431ecbc253e559a628ed5d3de39..95782a450348f13eaaea74ac350db8fe00449860 100644 (file)
@@ -692,7 +692,7 @@ algorithm_from_tag (char *s)
   /* Terminate tag, and lookup.  */
   char sep = s[i];
   s[i] = '\0';
-  ptrdiff_t algo = argmatch (s, algorithm_tags, NULL, 0);
+  ptrdiff_t algo = argmatch_exact (s, algorithm_tags);
   s[i] = sep;
 
   return algo;
@@ -1286,8 +1286,8 @@ main (int argc, char **argv)
       {
 #if HASH_ALGO_CKSUM
       case 'a':
-        cksum_algorithm = XARGMATCH ("--algorithm", optarg,
-                                     algorithm_args, algorithm_types);
+        cksum_algorithm = XARGMATCH_EXACT ("--algorithm", optarg,
+                                           algorithm_args, algorithm_types);
         algorithm_specified = true;
         break;
 
index 3774e61d59f50e131ee26f32034f36760774afc8..34b6d4d93895e77993342f52f00b1f4de2c78bda 100755 (executable)
@@ -47,6 +47,10 @@ while read algo prog; do
 done < input_options
 compare out out-a || fail=1
 
+# Ensure --check not allowed with older (non tagged) algorithms
 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
+
 Exit $fail