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

fix(parser): Don't panic on invalid UTF-8 values #4474

Merged
merged 2 commits into from Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion src/lib.rs
Expand Up @@ -156,4 +156,3 @@ mod util;

const INTERNAL_ERROR_MSG: &str = "Fatal internal error. Please consider filing a bug \
report at https://github.com/clap-rs/clap/issues";
const INVALID_UTF8: &str = "unexpected invalid UTF-8 code point";
2 changes: 1 addition & 1 deletion src/parser/features/suggestions.rs
Expand Up @@ -35,7 +35,7 @@ where
/// Returns a suffix that can be empty, or is the standard 'did you mean' phrase
pub(crate) fn did_you_mean_flag<'a, 'help, I, T>(
arg: &str,
remaining_args: &[&str],
remaining_args: &[&std::ffi::OsStr],
longs: I,
subcommands: impl IntoIterator<Item = &'a mut Command>,
) -> Option<(String, Option<String>)>
Expand Down
10 changes: 4 additions & 6 deletions src/parser/parser.rs
Expand Up @@ -20,7 +20,7 @@ use crate::parser::{ArgMatcher, SubCommand};
use crate::parser::{Validator, ValueSource};
use crate::util::Id;
use crate::ArgAction;
use crate::{INTERNAL_ERROR_MSG, INVALID_UTF8};
use crate::INTERNAL_ERROR_MSG;

pub(crate) struct Parser<'cmd> {
cmd: &'cmd mut Command,
Expand Down Expand Up @@ -171,10 +171,8 @@ impl<'cmd> Parser<'cmd> {
}
ParseResult::NoMatchingArg { arg } => {
let _ = self.resolve_pending(matcher);
let remaining_args: Vec<_> = raw_args
.remaining(&mut args_cursor)
.map(|x| x.to_str().expect(INVALID_UTF8))
.collect();
let remaining_args: Vec<_> =
raw_args.remaining(&mut args_cursor).collect();
return Err(self.did_you_mean_error(
&arg,
matcher,
Expand Down Expand Up @@ -1523,7 +1521,7 @@ impl<'cmd> Parser<'cmd> {
&mut self,
arg: &str,
matcher: &mut ArgMatcher,
remaining_args: &[&str],
remaining_args: &[&OsStr],
trailing_values: bool,
) -> ClapError {
debug!("Parser::did_you_mean_error: arg={}", arg);
Expand Down
22 changes: 22 additions & 0 deletions tests/builder/utf8.rs
Expand Up @@ -100,6 +100,28 @@ fn invalid_utf8_strict_option_long_equals() {
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
}

#[test]
fn invalid_utf8_strict_invalid_short() {
let m = Command::new("bad_utf8").try_get_matches_from(vec![
OsString::from(""),
OsString::from("-a"),
OsString::from_vec(vec![0xe9]),
]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument);
}

#[test]
fn invalid_utf8_strict_invalid_long() {
let m = Command::new("bad_utf8").try_get_matches_from(vec![
OsString::from(""),
OsString::from("--arg"),
OsString::from_vec(vec![0xe9]),
]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument);
}

#[test]
fn invalid_utf8_positional() {
let r = Command::new("bad_utf8")
Expand Down