{"record":{"id":"41961ad7ccfd001d","repo":"cross-rs/cross","slug":"commit-hash-must-be-at-least-length-characters-long","errorCode":null,"errorMessage":"commit hash must be at least {LENGTH} characters long","messagePattern":"commit hash must be at least (.+?) characters long","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/rustc.rs","lineNumber":54,"sourceCode":"    fn needs_interpreter(&self) -> bool {\n        self.semver < Version::new(1, 19, 0)\n    }\n\n    fn commit_hash(&self) -> String {\n        self.commit_hash.as_ref().map_or_else(\n            || hash_from_version_string(&self.short_version_string, 2),\n            |x| short_commit_hash(x),\n        )\n    }\n}\n\nfn short_commit_hash(hash: &str) -> String {\n    // short version hashes are always 9 digits\n    //  https://github.com/rust-lang/cargo/pull/10579\n    const LENGTH: usize = 9;\n\n    hash.get(..LENGTH)\n        .unwrap_or_else(|| panic!(\"commit hash must be at least {LENGTH} characters long\"))\n        .to_owned()\n}\n\n#[must_use]\npub fn hash_from_version_string(version: &str, index: usize) -> String {\n    let is_hash = |x: &str| x.chars().all(|c| c.is_ascii_hexdigit());\n    let is_date = |x: &str| x.chars().all(|c| matches!(c, '-' | '0'..='9'));\n\n    // the version can be one of two forms:\n    //   multirust channel string: `\"1.61.0 (fe5b13d68 2022-05-18)\"`\n    //   short version string: `\"rustc 1.61.0 (fe5b13d68 2022-05-18)\"`\n    // want to extract the commit hash if we can, if not, just hash the string.\n    if let Some((commit, date)) = version\n        .splitn(index + 1, ' ')\n        .nth(index)\n        .and_then(|meta| meta.strip_prefix('('))\n        .and_then(|meta| meta.strip_suffix(')'))\n        .and_then(|meta| meta.split_once(' '))","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/cross-rs/cross/blob/8c1a8aa4b661711f4b7b6ac07c2e8929ce2f7d27/src/rustc.rs#L36-L72","documentation":"`short_commit_hash` truncates a commit hash to the 9-character short form used by Cargo (rust-lang/cargo#10579). It panics when the input string is shorter than 9 characters, since `str::get(..9)` returns None. This is a guard against garbage/short identifiers being treated as commit hashes.","triggerScenarios":"Calling `short_commit_hash` (directly or via `commit_hash` or `hash_from_version_string`) with a string of fewer than 9 ASCII characters, e.g. a version metadata field like \"abc123\".","commonSituations":"A rustc version string whose commit hash portion was truncated or hand-edited, a custom/patched toolchain reporting a short or missing commit hash in its `-v` output, or parsing an incorrectly formatted version string.","solutions":["Verify the input is a full commit hash: run `rustc -vV` and check the `commit-hash:` line has at least 9 hex characters.","Validate length before calling: `assert!(hash.len() >= 9)` or return a proper error instead of panicking.","If the version string is hand-assembled, include the full commit hash rather than an abbreviated one."],"exampleFix":"// before\nlet short = short_commit_hash(\"abc123\"); // panics: only 6 chars\n// after\nlet hash = \"abc123def\";\nassert!(hash.len() >= 9);\nlet short = short_commit_hash(hash); // \"abc123def\"","handlingStrategy":"validation","validationCode":"fn is_valid_commit_hash(hash: &str) -> bool { hash.len() >= 9 && hash.chars().all(|c| c.is_ascii_hexdigit()) }\nif !is_valid_commit_hash(input) { return Err(...); }\nlet short = short_commit_hash(input);","typeGuard":"fn as_full_commit_hash(s: &str) -> Option<&str> { (s.len() >= 9 && s.chars().all(|c| c.is_ascii_hexdigit())).then_some(s) }","tryCatchPattern":null,"preventionTips":["Only feed commit hashes taken verbatim from `rustc -vV` output.","Assert hash length >= 9 in tests that exercise version parsing.","Never truncate commit hashes before passing them to this API."],"tags":["panic","commit-hash","version-parsing","rustc"],"backgroundTag":"invalid-identifier-format","analyzedSha":"8c1a8aa4b661711f4b7b6ac07c2e8929ce2f7d27","analyzedAt":"2026-09-13T15:10:43.988Z","contentChangedAt":"2026-09-13T15:10:43.988Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}