Skip to content

Commit 61cb6ee

Browse files
committed
fix: do not panic when failed to parse rustc commit-hash
In some situation the commit-hash in `rustc -vV` output is "unknown". The debug assertion must not block any progress on others like miri, so removed and log instead.
1 parent fa62a0e commit 61cb6ee

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/cargo/util/rustc.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,22 @@ impl Rustc {
8282
verbose_version
8383
)
8484
})?;
85-
let commit_hash = extract("commit-hash: ").ok().map(|hash| {
86-
debug_assert!(
87-
hash.chars().all(|ch| ch.is_ascii_hexdigit()),
88-
"commit hash must be a hex string, got: {hash:?}"
89-
);
90-
debug_assert!(
91-
hash.len() == 40 || hash.len() == 64,
92-
"hex string must be generated from sha1 or sha256 (i.e., it must be 40 or 64 characters long)\ngot: {hash:?}"
93-
);
94-
hash.to_string()
95-
});
85+
let commit_hash = extract("commit-hash: ").ok()
86+
.filter(|hash| {
87+
// Ensure that commit hash is always a SHA hex string.
88+
// If it is not, like `commit-hash: unknown`,
89+
// then treat it as if the entire `commit-hash:` line never exist.
90+
if !hash.chars().all(|ch| ch.is_ascii_hexdigit()) {
91+
debug!("commit hash must be a hex string, got: {hash:?}");
92+
return false;
93+
}
94+
if hash.len() != 40 && hash.len() != 64 {
95+
debug!("hex string must be generated from sha1 or sha256 (i.e., it must be 40 or 64 characters long)\ngot: {hash:?}");
96+
return false;
97+
}
98+
true
99+
})
100+
.map(|hash| hash.to_string());
96101

97102
Ok(Rustc {
98103
path,

0 commit comments

Comments
 (0)