Skip to content

Commit 76c1ed1

Browse files
committed
fix(cli): Forward non-UTF8 arguments to external subcommands
I noticed this because clap v4 changed the default for external subcommands from `String` to `OsString` with the assumption that this would push people to "do the right thing" more often.
1 parent e320c3e commit 76c1ed1

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/bin/cargo/cli.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use cargo::{self, drop_print, drop_println, CliResult, Config};
55
use clap::{AppSettings, Arg, ArgMatches};
66
use itertools::Itertools;
77
use std::collections::HashMap;
8+
use std::ffi::OsStr;
9+
use std::ffi::OsString;
810
use std::fmt::Write;
911

1012
use super::commands;
@@ -241,14 +243,14 @@ fn expand_aliases(
241243
}
242244
(Some(_), None) => {
243245
// Command is built-in and is not conflicting with alias, but contains ignored values.
244-
if let Some(mut values) = args.get_many::<String>("") {
246+
if let Some(values) = args.get_many::<OsString>("") {
245247
return Err(anyhow::format_err!(
246248
"\
247249
trailing arguments after built-in command `{}` are unsupported: `{}`
248250
249251
To pass the arguments to the subcommand, remove `--`",
250252
cmd,
251-
values.join(" "),
253+
values.map(|s| s.to_string_lossy()).join(" "),
252254
)
253255
.into());
254256
}
@@ -270,7 +272,7 @@ For more information, see issue #10049 <https://github.com/rust-lang/cargo/issue
270272
))?;
271273
}
272274

273-
alias.extend(args.get_many::<String>("").unwrap_or_default().cloned());
275+
alias.extend(args.get_many::<OsString>("").unwrap_or_default().cloned());
274276
// new_args strips out everything before the subcommand, so
275277
// capture those global options now.
276278
// Note that an alias to an external command will not receive
@@ -346,12 +348,12 @@ fn execute_subcommand(config: &mut Config, cmd: &str, subcommand_args: &ArgMatch
346348
return exec(config, subcommand_args);
347349
}
348350

349-
let mut ext_args: Vec<&str> = vec![cmd];
351+
let mut ext_args: Vec<&OsStr> = vec![OsStr::new(cmd)];
350352
ext_args.extend(
351353
subcommand_args
352-
.get_many::<String>("")
354+
.get_many::<OsString>("")
353355
.unwrap_or_default()
354-
.map(String::as_str),
356+
.map(OsString::as_os_str),
355357
);
356358
super::execute_external_subcommand(config, cmd, &ext_args)
357359
}
@@ -400,6 +402,7 @@ pub fn cli() -> App {
400402
};
401403
App::new("cargo")
402404
.allow_external_subcommands(true)
405+
.allow_invalid_utf8_for_external_subcommands(true)
403406
.setting(AppSettings::DeriveDisplayOrder)
404407
// Doesn't mix well with our list of common cargo commands. See clap-rs/clap#3108 for
405408
// opening clap up to allow us to style our help template

src/bin/cargo/commands/help.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use cargo::util::errors::CargoResult;
44
use cargo::{drop_println, Config};
55
use cargo_util::paths::resolve_executable;
66
use flate2::read::GzDecoder;
7+
use itertools::Itertools;
8+
use std::ffi::OsStr;
79
use std::ffi::OsString;
810
use std::io::Read;
911
use std::io::Write;
@@ -21,7 +23,11 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
2123
let subcommand = args.get_one::<String>("SUBCOMMAND");
2224
if let Some(subcommand) = subcommand {
2325
if !try_help(config, subcommand)? {
24-
crate::execute_external_subcommand(config, subcommand, &[subcommand, "--help"])?;
26+
crate::execute_external_subcommand(
27+
config,
28+
subcommand,
29+
&[OsStr::new(subcommand), OsStr::new("--help")],
30+
)?;
2531
}
2632
} else {
2733
let mut cmd = crate::cli::cli();
@@ -34,14 +40,20 @@ fn try_help(config: &Config, subcommand: &str) -> CargoResult<bool> {
3440
let subcommand = match check_alias(config, subcommand) {
3541
// If this alias is more than a simple subcommand pass-through, show the alias.
3642
Some(argv) if argv.len() > 1 => {
37-
let alias = argv.join(" ");
43+
let alias = argv
44+
.into_iter()
45+
.map(|s| s.to_string_lossy().into_owned())
46+
.join(" ");
3847
drop_println!(config, "`{}` is aliased to `{}`", subcommand, alias);
3948
return Ok(true);
4049
}
4150
// Otherwise, resolve the alias into its subcommand.
4251
Some(argv) => {
4352
// An alias with an empty argv can be created via `"empty-alias" = ""`.
44-
let first = argv.get(0).map(String::as_str).unwrap_or(subcommand);
53+
let first = argv
54+
.get(0)
55+
.map(|s| s.to_str().expect("aliases are always UTF-8"))
56+
.unwrap_or(subcommand);
4557
first.to_string()
4658
}
4759
None => subcommand.to_string(),
@@ -77,7 +89,7 @@ fn try_help(config: &Config, subcommand: &str) -> CargoResult<bool> {
7789
/// Checks if the given subcommand is an alias.
7890
///
7991
/// Returns None if it is not an alias.
80-
fn check_alias(config: &Config, subcommand: &str) -> Option<Vec<String>> {
92+
fn check_alias(config: &Config, subcommand: &str) -> Option<Vec<OsString>> {
8193
aliased_command(config, subcommand).ok().flatten()
8294
}
8395

src/bin/cargo/main.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use cargo::util::{self, closest_msg, command_prelude, CargoResult, CliResult, Co
77
use cargo_util::{ProcessBuilder, ProcessError};
88
use std::collections::BTreeMap;
99
use std::env;
10+
use std::ffi::OsStr;
11+
use std::ffi::OsString;
1012
use std::fs;
1113
use std::path::{Path, PathBuf};
1214

@@ -52,7 +54,7 @@ fn builtin_aliases_execs(cmd: &str) -> Option<&(&str, &str, &str)> {
5254
BUILTIN_ALIASES.iter().find(|alias| alias.0 == cmd)
5355
}
5456

55-
fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<String>>> {
57+
fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<OsString>>> {
5658
let alias_name = format!("alias.{}", command);
5759
let user_alias = match config.get_string(&alias_name) {
5860
Ok(Some(record)) => Some(
@@ -66,9 +68,11 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<Str
6668
Err(_) => config.get::<Option<Vec<String>>>(&alias_name)?,
6769
};
6870

69-
let result = user_alias.or_else(|| {
70-
builtin_aliases_execs(command).map(|command_str| vec![command_str.1.to_string()])
71-
});
71+
let result = user_alias
72+
.or_else(|| {
73+
builtin_aliases_execs(command).map(|command_str| vec![command_str.1.to_string()])
74+
})
75+
.map(|v| v.into_iter().map(|s| OsString::from(s)).collect());
7276
Ok(result)
7377
}
7478

@@ -152,7 +156,7 @@ fn find_external_subcommand(config: &Config, cmd: &str) -> Option<PathBuf> {
152156
.find(|file| is_executable(file))
153157
}
154158

155-
fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult {
159+
fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&OsStr]) -> CliResult {
156160
let path = find_external_subcommand(config, cmd);
157161
let command = match path {
158162
Some(command) => command,

0 commit comments

Comments
 (0)