Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some validation to rustc-link-arg #9523

Merged
merged 3 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Make cargo:rustc-link-arg-bin without the = an error.
  • Loading branch information
ehuss committed May 29, 2021
commit 836e537bc0413d8cf9cefc006ab119ddc82cb0a0
47 changes: 25 additions & 22 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,29 +599,32 @@ impl BuildOutput {
}
"rustc-link-arg-bin" => {
if extra_link_arg {
let parts = value.splitn(2, "=").collect::<Vec<_>>();
if parts.len() == 2 {
let bin_name = parts[0].to_string();
if !targets
.iter()
.any(|target| target.is_bin() && target.name() == bin_name)
{
bail!(
"invalid instruction `cargo:{}` from {}\n\
The package {} does not have a bin target with the name `{}`.",
key,
whence,
pkg_descr,
bin_name
);
}
linker_args.push((LinkType::SingleBin(bin_name), parts[1].to_string()));
} else {
warnings.push(format!(
"cargo:{} has invalid syntax: expected `cargo:{}=BIN=ARG`",
key, key
));
let mut parts = value.splitn(2, '=');
let bin_name = parts.next().unwrap().to_string();
let arg = parts.next().ok_or_else(|| {
anyhow::format_err!(
"invalid instruction `cargo:{}={}` from {}\n\
The instruction should have the form cargo:{}=BIN=ARG",
key,
value,
whence,
key
)
})?;
if !targets
.iter()
.any(|target| target.is_bin() && target.name() == bin_name)
{
bail!(
"invalid instruction `cargo:{}` from {}\n\
The package {} does not have a bin target with the name `{}`.",
key,
whence,
pkg_descr,
bin_name
);
}
linker_args.push((LinkType::SingleBin(bin_name), arg.to_string()));
} else {
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
}
Expand Down
17 changes: 17 additions & 0 deletions tests/testsuite/build_script_extra_link_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target.
[COMPILING] foo [..]
error: invalid instruction `cargo:rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)`
The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `abc`.
",
)
.run();

p.change_file(
"build.rs",
r#"fn main() { println!("cargo:rustc-link-arg-bin=abc"); }"#,
);

p.cargo("check -Zextra-link-arg")
.masquerade_as_nightly_cargo()
.with_status(101)
.with_stderr(
"\
[COMPILING] foo [..]
error: invalid instruction `cargo:rustc-link-arg-bin=abc` from build script of `foo v0.0.1 ([ROOT]/foo)`
The instruction should have the form cargo:rustc-link-arg-bin=BIN=ARG
",
)
.run();
Expand Down