Skip to content

Commit

Permalink
fix(parser): Don't panic on invalid UTF-8 values
Browse files Browse the repository at this point in the history
Fixes #4473
  • Loading branch information
epage committed Nov 11, 2022
1 parent 45d26e0 commit e9cbed3
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 9 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
1 change: 0 additions & 1 deletion tests/builder/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ fn invalid_utf8_strict_invalid_short() {
}

#[test]
#[should_panic]
fn invalid_utf8_strict_invalid_long() {
let m = Command::new("bad_utf8").try_get_matches_from(vec![
OsString::from(""),
Expand Down

0 comments on commit e9cbed3

Please sign in to comment.