From dd3f434d0ddced8e29da4dab397c11f706c5a54a Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 7 Nov 2023 20:03:10 +0800 Subject: [PATCH 1/2] Do not allow empty feature name Signed-off-by: hi-rustin --- src/cargo/core/summary.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cargo/core/summary.rs b/src/cargo/core/summary.rs index 128c0db9cb1..0dc55c3cf75 100644 --- a/src/cargo/core/summary.rs +++ b/src/cargo/core/summary.rs @@ -431,6 +431,9 @@ impl fmt::Display for FeatureValue { pub type FeatureMap = BTreeMap>; 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)) { @@ -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()); } } From d61816465f25b918acf8800afd8cbbd1c6d73c89 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 7 Nov 2023 20:03:35 +0800 Subject: [PATCH 2/2] Add test for empty feature name Signed-off-by: hi-rustin --- tests/testsuite/features.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/testsuite/features.rs b/tests/testsuite/features.rs index 236b6f65181..e44807941d1 100644 --- a/tests/testsuite/features.rs +++ b/tests/testsuite/features.rs @@ -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.