Skip to content

Commit 1791353

Browse files
committed
fix!: Disallow empty registry names
This was allowed when switching from `toml` v0.5 to `toml_edit` which started allowing empty keys when parsing TOML. This mirrors the change we made for disallowing empty feature names in #12928.
1 parent 7200646 commit 1791353

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/cargo/util/command_prelude.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,9 @@ Run `{cmd}` to see possible targets."
833833
(None, None) => config.default_registry()?.map(RegistryOrIndex::Registry),
834834
(None, Some(i)) => Some(RegistryOrIndex::Index(i.into_url()?)),
835835
(Some(r), None) => {
836+
if r.is_empty() {
837+
bail!("registry name cannot be empty");
838+
}
836839
restricted_names::validate_package_name(r, "registry name", "")?;
837840
Some(RegistryOrIndex::Registry(r.to_string()))
838841
}
@@ -848,6 +851,9 @@ Run `{cmd}` to see possible targets."
848851
match self._value_of("registry").map(|s| s.to_string()) {
849852
None => config.default_registry(),
850853
Some(registry) => {
854+
if registry.is_empty() {
855+
bail!("registry name cannot be empty");
856+
}
851857
restricted_names::validate_package_name(&registry, "registry name", "")?;
852858
Ok(Some(registry))
853859
}

src/cargo/util/config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,9 @@ impl Config {
15511551

15521552
/// Gets the index for a registry.
15531553
pub fn get_registry_index(&self, registry: &str) -> CargoResult<Url> {
1554+
if registry.is_empty() {
1555+
bail!("registry name cannot be empty");
1556+
}
15541557
validate_package_name(registry, "registry name", "")?;
15551558
if let Some(index) = self.get_string(&format!("registries.{}.index", registry))? {
15561559
self.resolve_registry_index(&index).with_context(|| {

tests/testsuite/alt_registry.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ fn config_empty_registry_name() {
15681568
.with_status(101)
15691569
.with_stderr(
15701570
"\
1571-
[ERROR] registry index was not found in any configuration: ``",
1571+
[ERROR] registry name cannot be empty",
15721572
)
15731573
.run();
15741574
}
@@ -1583,7 +1583,7 @@ fn empty_registry_flag() {
15831583
.with_status(101)
15841584
.with_stderr(
15851585
"\
1586-
[ERROR] registry index was not found in any configuration: ``",
1586+
[ERROR] registry name cannot be empty",
15871587
)
15881588
.run();
15891589
}
@@ -1618,7 +1618,7 @@ fn empty_dependency_registry() {
16181618
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
16191619
16201620
Caused by:
1621-
registry index was not found in any configuration: ``",
1621+
registry name cannot be empty",
16221622
)
16231623
.run();
16241624
}

0 commit comments

Comments
 (0)