Assert that Dependency::name is never empty, prevent 'install ""' from crashing

An explicit `cargo install ""` would cause clap to pass an empty crate-name,
leading to a panic(). We now assert() that Dependency::name is never the
empty string and prevent the situation in the first place by not allowing
the crate-name to be empty for `install`.

Fixes #5229
This commit is contained in:
Lukas Lueg 2018-03-23 15:26:50 +01:00
parent c157d7dd7a
commit 81ed0620bc
4 changed files with 20 additions and 1 deletions

View File

@ -7,7 +7,7 @@ use cargo::util::ToUrl;
pub fn cli() -> App {
subcommand("install")
.about("Install a Rust binary")
.arg(Arg::with_name("crate").multiple(true))
.arg(Arg::with_name("crate").empty_values(false).multiple(true))
.arg(
opt("version", "Specify a version to install from crates.io")
.alias("vers")

View File

@ -185,6 +185,7 @@ impl Dependency {
}
pub fn new_override(name: &str, source_id: &SourceId) -> Dependency {
assert!(name.len() > 0);
Dependency {
inner: Rc::new(Inner {
name: InternedString::new(name),

View File

@ -1476,3 +1476,14 @@ dependencies = [
assert_that(cargo_process("install").arg("foo"), execs().with_status(0));
}
#[test]
fn install_empty_argument() {
// Bug 5229
assert_that(
cargo_process("install").arg(""),
execs().with_status(1).with_stderr_contains(
"[ERROR] The argument '<crate>...' requires a value but none was supplied",
),
);
}

View File

@ -172,6 +172,13 @@ fn loc_names(names: &[(&'static str, &'static str)]) -> Vec<PackageId> {
.collect()
}
#[test]
#[should_panic(expected = "assertion failed: name.len() > 0")]
fn test_dependency_with_empty_name() {
// Bug 5229, dependency-names must not be empty
"".to_dep();
}
#[test]
fn test_resolving_empty_dependency_list() {
let res = resolve(&pkg_id("root"), Vec::new(), &registry(vec![])).unwrap();