Skip to content

Commit 9200747

Browse files
committed
cksum: validate options more consistently
We disallow `cksum --tag --check` which is fine, but the error should be consistent with md5sum, and less confusing, as it currently mentions "--binary" and "--text" which weren't specified. We disallow `cksum --tag --text` which is fine, but we should also disallow `cksum --text --tag`. We should honor an explicit --binary (output *) with this combination of options: cksum --binary --tag --untagged -a md5 /dev/null Note this also makes both of `cksum -a md5` and `cksum --tag -a md5` consistently use binary mode when reading from a tty on systems like MinGW where O_BINARY is set. * src/cksum.c (main): Adjust --text,--binary and --tag,--untagged option processing. * tests/cksum/cksum-a.sh: Add test cases. * tests/cksum/cksum-c.sh: Likewise. * NEWS: Mention the improvement. Fixes #163
1 parent 9a38863 commit 9200747

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ GNU coreutils NEWS -*- outline -*-
7070
is reduced in size by 3.2% through the more efficient reuse of the cksum
7171
utility by the md5sum and sha*sum utilities.
7272

73+
'cksum' now validates its options more consistently.
74+
E.g., `cksum --text --tag` now fails like `cksum --tag --text` already did.
75+
7376
csplit, ls, and sort, now handle a more complete set of terminating signals.
7477

7578
'du' now processes directories with 10,000 or more entries up to 9 times

src/cksum.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,14 +1623,11 @@ main (int argc, char **argv)
16231623
raw_digest = true;
16241624
break;
16251625
case UNTAG_OPTION:
1626-
if (prefix_tag == 1)
1627-
binary = -1;
16281626
prefix_tag = 0;
16291627
break;
16301628
# endif
16311629
case TAG_OPTION:
16321630
prefix_tag = 1;
1633-
binary = 1;
16341631
break;
16351632
case 'z':
16361633
digest_delim = '\0';
@@ -1735,38 +1732,20 @@ main (int argc, char **argv)
17351732
}
17361733
#endif
17371734

1738-
if (prefix_tag == -1)
1739-
prefix_tag = HASH_ALGO_CKSUM;
1740-
1741-
if (prefix_tag && !binary)
1742-
{
1743-
/* This could be supported in a backwards compatible way
1744-
by prefixing the output line with a space in text mode.
1745-
However that's invasive enough that it was agreed to
1746-
not support this mode with --tag, as --text use cases
1747-
are adequately supported by the default output format. */
1748-
#if !HASH_ALGO_CKSUM
1749-
error (0, 0, _("--tag does not support --text mode"));
1750-
#else
1751-
error (0, 0, _("--text mode is only supported with --untagged"));
1752-
#endif
1753-
usage (EXIT_FAILURE);
1754-
}
1755-
17561735
if (digest_delim != '\n' && do_check)
17571736
{
17581737
error (0, 0, _("the --zero option is not supported when "
17591738
"verifying checksums"));
17601739
usage (EXIT_FAILURE);
17611740
}
1762-
#if !HASH_ALGO_CKSUM
1763-
if (prefix_tag && do_check)
1741+
if (1 <= prefix_tag && do_check)
17641742
{
1743+
/* Note we allow --untagged with --check to more
1744+
seamlessly support --untagged in an emulation wrapper. */
17651745
error (0, 0, _("the --tag option is meaningless when "
17661746
"verifying checksums"));
17671747
usage (EXIT_FAILURE);
17681748
}
1769-
#endif
17701749

17711750
if (0 <= binary && do_check)
17721751
{
@@ -1811,8 +1790,28 @@ main (int argc, char **argv)
18111790
usage (EXIT_FAILURE);
18121791
}
18131792

1793+
if (prefix_tag == -1)
1794+
prefix_tag = HASH_ALGO_CKSUM;
1795+
1796+
if (prefix_tag && !binary)
1797+
{
1798+
/* This could be supported in a backwards compatible way
1799+
by prefixing the output line with a space in text mode.
1800+
However that's invasive enough that it was agreed to
1801+
not support this mode with --tag, as --text use cases
1802+
are adequately supported by the default output format. */
1803+
#if !HASH_ALGO_CKSUM
1804+
error (0, 0, _("--tag does not support --text mode"));
1805+
#else
1806+
error (0, 0, _("--text mode is only supported with --untagged"));
1807+
#endif
1808+
usage (EXIT_FAILURE);
1809+
}
1810+
18141811
if (!O_BINARY && binary < 0)
18151812
binary = 0;
1813+
else if (prefix_tag)
1814+
binary = 1;
18161815

18171816
char **operand_lim = argv + argc;
18181817
if (optind == argc)

tests/cksum/cksum-a.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,23 @@ returns_ 1 cksum -a bsd --check </dev/null || fail=1
6868
# Ensure abbreviations not supported for algorithm selection
6969
returns_ 1 cksum -a sha22 </dev/null || fail=1
7070

71+
# Ensure --text and --tag combo is disallowed
72+
returns_ 1 cksum --text --tag -a md5 </dev/null || fail=1
73+
returns_ 1 cksum --tag --text -a md5 </dev/null || fail=1
74+
7175
# Ensure --tag -> --untagged transition resets binary indicator
7276
cksum --tag --untagged -a md5 /dev/null >out-1 || fail=1
73-
# --binary ignored in this edge case
74-
cksum --binary --tag --untagged -a md5 /dev/null >out-2 || fail=1
7577
# base case for comparison
7678
cksum --untagged -a md5 /dev/null >out || fail=1
7779
compare out out-1 || fail=1
78-
compare out out-2 || fail=1
7980

8081
# check that the '*' is present
8182
cksum --tag --untagged --binary -a md5 /dev/null >out || fail=1
8283
grep " *" out >/dev/null || { fail=1; cat out; }
8384
# Verify that the order does not reset binary indicator
8485
cksum --binary --untagged -a md5 /dev/null >out-1 || fail=1
8586
compare out out-1 || fail=1
87+
cksum --binary --tag --untagged -a md5 /dev/null >out-2 || fail=1
88+
compare out out-2 || fail=1
8689

8790
Exit $fail

tests/cksum/cksum-c.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,13 @@ touch empty-1 empty-2 || framework_failure_
192192
cksum --check empty-1 empty-2 > out 2>&1 && fail=1
193193
grep 'empty-1: no properly formatted checksum lines found' out || fail=1
194194
grep 'empty-2: no properly formatted checksum lines found' out || fail=1
195+
196+
197+
# Disallow --tag --check, with appropriate error
198+
returns_ 1 cksum --tag --check /dev/null 2> err
199+
grep 'the --tag option is meaningless when verifying checksums' err || fail=1
200+
# Allow --untagged --check, to better support
201+
# an emulation wrapper for legacy *sum utils.
202+
cksum -a md5 /dev/null | cksum --untagged --check || fail=1
203+
195204
Exit $fail

0 commit comments

Comments
 (0)