From e9cbed34cd12728b79dcdfc745ba7777e5d13029 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 11 Nov 2022 12:25:56 -0600 Subject: [PATCH] fix(parser): Don't panic on invalid UTF-8 values Fixes #4473 --- src/lib.rs | 1 - src/parser/features/suggestions.rs | 2 +- src/parser/parser.rs | 10 ++++------ tests/builder/utf8.rs | 1 - 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 81748c8d..62b09292 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"; diff --git a/src/parser/features/suggestions.rs b/src/parser/features/suggestions.rs index f0d47709..f0aa5026 100644 --- a/src/parser/features/suggestions.rs +++ b/src/parser/features/suggestions.rs @@ -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, ) -> Option<(String, Option)> diff --git a/src/parser/parser.rs b/src/parser/parser.rs index a73094b3..d6198ca5 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -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, @@ -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, @@ -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); diff --git a/tests/builder/utf8.rs b/tests/builder/utf8.rs index 08e6cf50..0b31a3c4 100644 --- a/tests/builder/utf8.rs +++ b/tests/builder/utf8.rs @@ -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(""),