Skip to content

Commit

Permalink
forbid unprefixed SHAs for toolchains
Browse files Browse the repository at this point in the history
  • Loading branch information
lqd committed Mar 18, 2024
1 parent 4dbd62a commit aedec6d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/toolchain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::*;
use crate::utils;
use regex::Regex;
use rustwide::Toolchain as RustwideToolchain;
use std::fmt;
use std::str::FromStr;
Expand Down Expand Up @@ -102,6 +103,12 @@ pub enum ToolchainParseError {
InvalidSourceName(String),
#[error("invalid toolchain flag: {0}")]
InvalidFlag(String),
#[error("invalid toolchain SHA: {0} is missing a `try#` or `master#` prefix")]
PrefixMissing(String),
}

lazy_static! {
static ref TOOLCHAIN_SHA_RE: Regex = Regex::new(r"^[a-f0-9]{40}$").unwrap();
}

impl FromStr for Toolchain {
Expand Down Expand Up @@ -130,6 +137,10 @@ impl FromStr for Toolchain {
}
} else if raw_source.is_empty() {
return Err(ToolchainParseError::EmptyName);
} else if TOOLCHAIN_SHA_RE.is_match(raw_source) {
// A common user error is unprefixed SHAs for the `start` or `end` toolchains, check for
// these here.
return Err(ToolchainParseError::PrefixMissing(raw_source.to_string()));
} else {
RustwideToolchain::dist(raw_source)
};
Expand Down Expand Up @@ -348,5 +359,6 @@ mod tests {
assert!(Toolchain::from_str("stable+donotusethisflag=ever").is_err());
assert!(Toolchain::from_str("stable+patch=").is_err());
assert!(Toolchain::from_str("try#1234+target=").is_err());
assert!(Toolchain::from_str("0000000000000000000000000000000000000000").is_err());
}
}

0 comments on commit aedec6d

Please sign in to comment.