Assert that Dependency::name is never empty, prevent 'install ""' from crashing
authorLukas Lueg <lukas.lueg@gmail.com>
Fri, 23 Mar 2018 14:26:50 +0000 (15:26 +0100)
committerLukas Lueg <lukas.lueg@gmail.com>
Fri, 23 Mar 2018 15:07:01 +0000 (16:07 +0100)
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

src/bin/commands/install.rs
src/cargo/core/dependency.rs
tests/testsuite/install.rs
tests/testsuite/resolve.rs

index a3a62fd6669bfd06241e2e90feebe5bd7a6f2309..a353a28217e43edd950430bf8fcde3bb44d800eb 100644 (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")
index 173be4fb09f3ee36042fee24195957e86a56ead5..20ef00c0f59016717488a9144a7098acb2a64342 100644 (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),
index 5acdb62929ed5453fb1c44174589599fd100b723..250429e1655efa5b56810c18f7552e760aac3fe5 100644 (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",
+        ),
+    );
+}
index 53d06a0e9a7f10f2aa8583f6630b280d388337fa..c8dfc4903d641068d8684a5e69e425ca76ab444f 100644 (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();