cksum: fix parsing error with tagged cheksum files (#11704)

* cksum: fix parsing error with tagged cheksum files

When passed the '-c'/'--check' flag, and parsing a checksum file in
the "tagged" format, cksum (symlinked to sha256sum, etc...) expects a
line that looks like this:

ShA256 (file.bin) = da39a3ee5e6b4b0d3255bfef95601890afd80709

If the hash algorithm at the beginning of the line (in the above case
SHA256) is missing, then cksum panics because it is attempts to use the
value of an array at index -1. This fix causes cksum to instead consider
the line a syntax error and ignore it, just as GNU cksum does. I also
added unit and integration tests to check for the above behaviour.

---------

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
Josh French
2026-04-20 14:51:01 -07:00
committed by GitHub
parent 6e9a8b8dab
commit 0366d3c657
2 changed files with 27 additions and 0 deletions
@@ -281,6 +281,11 @@ impl LineFormat {
// tagged format does not put a space before (filename)
let par_idx = rest.iter().position(|&b| b == b'(')?;
// If the parenthesis is the first character (minus whitespace, which has already been stripped out), then,
// it's not a validly formatted line.
if par_idx == 0 {
return None;
}
let sub_case = if rest[par_idx - 1] == b' ' {
SubCase::Posix
} else {
@@ -1041,6 +1046,10 @@ mod tests {
(b" MD5(weirdfilename6) = ) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
(b" MD5 (weirdfilename7)= )= fds65dsf46as5df4d6f54asds5d7f7g9", None),
(b" MD5 (weirdfilename8) = )= fds65dsf46as5df4d6f54asds5d7f7g9", None),
// test for missing algorithm
(b"(filename) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
(b"filename) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
(b"filename = fds65dsf46as5df4d6f54asds5d7f7g9", None),
];
// cspell:enable
+18
View File
@@ -627,6 +627,24 @@ fn test_check_sha2_tagged_missing_hint() {
.stderr_contains("no properly formatted checksum lines found");
}
#[test]
fn test_check_tagged_missing_algo() {
// When checking tagged lines, if the algorithm is missing, print an "improperly
// formatted" message rather than panic.
let (at, mut ucmd) = at_and_ucmd!();
at.touch("a");
let invalid1 = "(a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
let invalid2 = "a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
let invalid3 = "(a = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
ucmd.arg("-c")
.pipe_in(format!("{invalid1}\n{invalid2}\n{invalid3}"))
.fails()
.stderr_contains("no properly formatted checksum lines found");
}
#[rstest]
#[case::md5("md5", "d41d8cd98f00b204e9800998ecf8427e")]
#[case::sha1("sha1", "da39a3ee5e6b4b0d3255bfef95601890afd80709")]