Skip to content

Commit d3e27bc

Browse files
committed
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.
1 parent 44aff6b commit d3e27bc

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/uucore/src/lib/features/checksum/validate.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ impl LineFormat {
281281
// tagged format does not put a space before (filename)
282282

283283
let par_idx = rest.iter().position(|&b| b == b'(')?;
284+
// If the parenthesis is the first character (minus whitespace, which has already been stripped out), then,
285+
// it's not a validly formatted line.
286+
if par_idx < 1 {
287+
return None;
288+
}
284289
let sub_case = if rest[par_idx - 1] == b' ' {
285290
SubCase::Posix
286291
} else {
@@ -1041,6 +1046,10 @@ mod tests {
10411046
(b" MD5(weirdfilename6) = ) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
10421047
(b" MD5 (weirdfilename7)= )= fds65dsf46as5df4d6f54asds5d7f7g9", None),
10431048
(b" MD5 (weirdfilename8) = )= fds65dsf46as5df4d6f54asds5d7f7g9", None),
1049+
// test for missing algorithm
1050+
(b"(filename) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
1051+
(b"filename) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
1052+
(b"filename = fds65dsf46as5df4d6f54asds5d7f7g9", None),
10441053
];
10451054

10461055
// cspell:enable

tests/by-util/test_cksum.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,24 @@ fn test_check_sha2_tagged_missing_hint() {
627627
.stderr_contains("no properly formatted checksum lines found");
628628
}
629629

630+
#[test]
631+
fn test_check_tagged_missing_algo() {
632+
// When checking tagged lines, if the algorithm is missing, print an "improperly
633+
// formatted" message rather than panic.
634+
635+
let (at, mut ucmd) = at_and_ucmd!();
636+
at.touch("a");
637+
638+
let invalid1 = "(a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
639+
let invalid2 = "a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
640+
let invalid3 = "(a = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
641+
642+
ucmd.arg("-c")
643+
.pipe_in(format!("{invalid1}\n{invalid2}\n{invalid3}"))
644+
.fails()
645+
.stderr_contains("no properly formatted checksum lines found");
646+
}
647+
630648
#[rstest]
631649
#[case::md5("md5", "d41d8cd98f00b204e9800998ecf8427e")]
632650
#[case::sha1("sha1", "da39a3ee5e6b4b0d3255bfef95601890afd80709")]

0 commit comments

Comments
 (0)