Skip to content

Commit e9cbed3

Browse files
committed
fix(parser): Don't panic on invalid UTF-8 values
Fixes #4473
1 parent 45d26e0 commit e9cbed3

File tree

4 files changed

+5
-9
lines changed

4 files changed

+5
-9
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,3 @@ mod util;
156156

157157
const INTERNAL_ERROR_MSG: &str = "Fatal internal error. Please consider filing a bug \
158158
report at https://github.com/clap-rs/clap/issues";
159-
const INVALID_UTF8: &str = "unexpected invalid UTF-8 code point";

src/parser/features/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ where
3535
/// Returns a suffix that can be empty, or is the standard 'did you mean' phrase
3636
pub(crate) fn did_you_mean_flag<'a, 'help, I, T>(
3737
arg: &str,
38-
remaining_args: &[&str],
38+
remaining_args: &[&std::ffi::OsStr],
3939
longs: I,
4040
subcommands: impl IntoIterator<Item = &'a mut Command>,
4141
) -> Option<(String, Option<String>)>

src/parser/parser.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::parser::{ArgMatcher, SubCommand};
2020
use crate::parser::{Validator, ValueSource};
2121
use crate::util::Id;
2222
use crate::ArgAction;
23-
use crate::{INTERNAL_ERROR_MSG, INVALID_UTF8};
23+
use crate::INTERNAL_ERROR_MSG;
2424

2525
pub(crate) struct Parser<'cmd> {
2626
cmd: &'cmd mut Command,
@@ -171,10 +171,8 @@ impl<'cmd> Parser<'cmd> {
171171
}
172172
ParseResult::NoMatchingArg { arg } => {
173173
let _ = self.resolve_pending(matcher);
174-
let remaining_args: Vec<_> = raw_args
175-
.remaining(&mut args_cursor)
176-
.map(|x| x.to_str().expect(INVALID_UTF8))
177-
.collect();
174+
let remaining_args: Vec<_> =
175+
raw_args.remaining(&mut args_cursor).collect();
178176
return Err(self.did_you_mean_error(
179177
&arg,
180178
matcher,
@@ -1523,7 +1521,7 @@ impl<'cmd> Parser<'cmd> {
15231521
&mut self,
15241522
arg: &str,
15251523
matcher: &mut ArgMatcher,
1526-
remaining_args: &[&str],
1524+
remaining_args: &[&OsStr],
15271525
trailing_values: bool,
15281526
) -> ClapError {
15291527
debug!("Parser::did_you_mean_error: arg={}", arg);

tests/builder/utf8.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ fn invalid_utf8_strict_invalid_short() {
112112
}
113113

114114
#[test]
115-
#[should_panic]
116115
fn invalid_utf8_strict_invalid_long() {
117116
let m = Command::new("bad_utf8").try_get_matches_from(vec![
118117
OsString::from(""),

0 commit comments

Comments
 (0)