Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/uucore/src/lib/features/checksum/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 < 1 {
return None;
}
let sub_case = if rest[par_idx - 1] == b' ' {
SubCase::Posix
} else {
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Loading