1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-21 03:12:48 +02:00

cksum: use more exact selection of digest algorithms

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.
This commit is contained in:
Pádraig Brady
2022-01-30 20:19:48 +00:00
parent 703e0487e4
commit a42a039132
2 changed files with 7 additions and 3 deletions

View 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;

View File

@@ -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