From dc2d0c0fb18b3cfb27fc4bfe401ee69a2da93489 Mon Sep 17 00:00:00 2001 From: Zach Lute Date: Wed, 12 Sep 2018 19:39:16 -0700 Subject: [PATCH] Validate that the package name contains no invalid characters. Invalid characters are currently defined as alphanumeric, _, and -. This matches the rustc restrictions but is not as restrictive as `cargo new` or crates.io. Mostly this is just so there will be better error messages in the case where characters in the package name aren't valid path characters. --- src/cargo/util/toml/mod.rs | 10 ++++++++++ tests/testsuite/build.rs | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 9d8366856..aee7f135e 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -810,6 +810,16 @@ impl TomlManifest { bail!("package name cannot be an empty string") } + for c in package_name.chars() { + if c.is_alphanumeric() { + continue; + } + if c == '_' || c == '-' { + continue; + } + bail!("Invalid character `{}` in package name: `{}`", c, package_name) + } + let pkgid = project.to_package_id(source_id)?; let edition = if let Some(ref edition) = project.edition { diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 2a5fffd77..75ebaf98f 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -257,7 +257,7 @@ Caused by: } #[test] -fn cargo_compile_with_invalid_package_name() { +fn cargo_compile_with_empty_package_name() { let p = project() .file("Cargo.toml", &basic_manifest("", "0.0.0")) .build(); @@ -274,6 +274,24 @@ Caused by: ).run(); } +#[test] +fn cargo_compile_with_invalid_package_name() { + let p = project() + .file("Cargo.toml", &basic_manifest("foo::bar", "0.0.0")) + .build(); + + p.cargo("build") + .with_status(101) + .with_stderr( + "\ +[ERROR] failed to parse manifest at `[..]` + +Caused by: + Invalid character `:` in package name: `foo::bar` +", + ).run(); +} + #[test] fn cargo_compile_with_invalid_bin_target_name() { let p = project()