Skip to content

Commit

Permalink
Auto merge of #12928 - hi-rustin:rustin-patch-feature-name, r=epage
Browse files Browse the repository at this point in the history
Do not allow empty feature name
  • Loading branch information
bors committed Nov 8, 2023
2 parents 4270265 + d618164 commit fed84e0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cargo/core/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ impl fmt::Display for FeatureValue {
pub type FeatureMap = BTreeMap<InternedString, Vec<FeatureValue>>;

fn validate_feature_name(pkg_id: PackageId, name: &str) -> CargoResult<()> {
if name.is_empty() {
bail!("feature name cannot be empty");
}
let mut chars = name.chars();
if let Some(ch) = chars.next() {
if !(unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_' || ch.is_digit(10)) {
Expand Down Expand Up @@ -488,5 +491,6 @@ mod tests {
assert!(validate_feature_name(pkg_id, "?foo").is_err());
assert!(validate_feature_name(pkg_id, "ⒶⒷⒸ").is_err());
assert!(validate_feature_name(pkg_id, "a¼").is_err());
assert!(validate_feature_name(pkg_id, "").is_err());
}
}
31 changes: 31 additions & 0 deletions tests/testsuite/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ Caused by:
.run();
}

#[cargo_test]
fn empty_feature_name() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[features]
"" = []
"#,
)
.file("src/main.rs", "")
.build();

p.cargo("check")
.with_status(101)
.with_stderr(
"\
[ERROR] failed to parse manifest at `[..]`
Caused by:
feature name cannot be empty
",
)
.run();
}

#[cargo_test]
fn same_name() {
// Feature with the same name as a dependency.
Expand Down

0 comments on commit fed84e0

Please sign in to comment.