From: Pádraig Brady
Date: Sun, 30 Jan 2022 20:19:48 +0000 (+0000) Subject: cksum: use more exact selection of digest algorithms X-Git-Tag: v9.1~120 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a42a0391329313482e0cee61c78b69e6c1500cad;p=thirdparty%2Fcoreutils.git 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. --- diff --git a/src/digest.c b/src/digest.c index 6de30ecb56..95782a4503 100644 --- a/src/digest.c +++ b/src/digest.c @@ -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; diff --git a/tests/misc/cksum-a.sh b/tests/misc/cksum-a.sh index 3774e61d59..34b6d4d938 100755 --- a/tests/misc/cksum-a.sh +++ b/tests/misc/cksum-a.sh @@ -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