Skip to content

Commit

Permalink
Merge pull request #4474 from epage/utf8
Browse files Browse the repository at this point in the history
fix(parser): Don't panic on invalid UTF-8 values
  • Loading branch information
epage authored Nov 11, 2022
2 parents 4d69e56 + e9cbed3 commit e6a3529
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit e6a3529

Please sign in to comment.