forked from clap-rs/clap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Provide path to avoid UTF-8 panics
Before, validating UTF-8 was all-or-nothing and would cause a `panic` if someone used the right API with non-UTF-8 input. Now, all arguments are validated for UTF-8, unless opted-out. This ensures a non-panicing path forward at the cost of people using the builder API that previously did `value_of_os` need to now set this flag. Fixes clap-rs#751
- Loading branch information
Showing
14 changed files
with
526 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
#![cfg(not(windows))] | ||
|
||
use clap::{Clap, ErrorKind}; | ||
use std::ffi::OsString; | ||
use std::os::unix::ffi::OsStringExt; | ||
|
||
#[derive(Clap, Debug, PartialEq, Eq)] | ||
struct Positional { | ||
arg: String, | ||
} | ||
|
||
#[derive(Clap, Debug, PartialEq, Eq)] | ||
struct Named { | ||
#[clap(short, long)] | ||
arg: String, | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_strict_positional() { | ||
let m = Positional::try_parse_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_strict_option_short_space() { | ||
let m = Named::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from("-a"), | ||
OsString::from_vec(vec![0xe9]), | ||
]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_strict_option_short_equals() { | ||
let m = Named::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]), | ||
]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_strict_option_short_no_space() { | ||
let m = Named::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from_vec(vec![0x2d, 0x61, 0xe9]), | ||
]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_strict_option_long_space() { | ||
let m = Named::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from("--arg"), | ||
OsString::from_vec(vec![0xe9]), | ||
]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_strict_option_long_equals() { | ||
let m = Named::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]), | ||
]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); | ||
} | ||
|
||
#[derive(Clap, Debug, PartialEq, Eq)] | ||
struct PositionalOs { | ||
#[clap(parse(from_os_str))] | ||
arg: OsString, | ||
} | ||
|
||
#[derive(Clap, Debug, PartialEq, Eq)] | ||
struct NamedOs { | ||
#[clap(short, long, parse(from_os_str))] | ||
arg: OsString, | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_positional() { | ||
let r = PositionalOs::try_parse_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]); | ||
assert_eq!( | ||
r.unwrap(), | ||
PositionalOs { | ||
arg: OsString::from_vec(vec![0xe9]) | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_option_short_space() { | ||
let r = NamedOs::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from("-a"), | ||
OsString::from_vec(vec![0xe9]), | ||
]); | ||
assert_eq!( | ||
r.unwrap(), | ||
NamedOs { | ||
arg: OsString::from_vec(vec![0xe9]) | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_option_short_equals() { | ||
let r = NamedOs::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]), | ||
]); | ||
assert_eq!( | ||
r.unwrap(), | ||
NamedOs { | ||
arg: OsString::from_vec(vec![0xe9]) | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_option_short_no_space() { | ||
let r = NamedOs::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from_vec(vec![0x2d, 0x61, 0xe9]), | ||
]); | ||
assert_eq!( | ||
r.unwrap(), | ||
NamedOs { | ||
arg: OsString::from_vec(vec![0xe9]) | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_option_long_space() { | ||
let r = NamedOs::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from("--arg"), | ||
OsString::from_vec(vec![0xe9]), | ||
]); | ||
assert_eq!( | ||
r.unwrap(), | ||
NamedOs { | ||
arg: OsString::from_vec(vec![0xe9]) | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn invalid_utf8_option_long_equals() { | ||
let r = NamedOs::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]), | ||
]); | ||
assert_eq!( | ||
r.unwrap(), | ||
NamedOs { | ||
arg: OsString::from_vec(vec![0xe9]) | ||
} | ||
); | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clap)] | ||
enum External { | ||
#[clap(external_subcommand)] | ||
Other(Vec<String>), | ||
} | ||
|
||
#[test] | ||
fn refuse_invalid_utf8_subcommand_with_allow_external_subcommands() { | ||
let m = External::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from_vec(vec![0xe9]), | ||
OsString::from("normal"), | ||
]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); | ||
} | ||
|
||
#[test] | ||
fn refuse_invalid_utf8_subcommand_args_with_allow_external_subcommands() { | ||
let m = External::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from("subcommand"), | ||
OsString::from("normal"), | ||
OsString::from_vec(vec![0xe9]), | ||
OsString::from("--another_normal"), | ||
]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clap)] | ||
enum ExternalOs { | ||
#[clap(external_subcommand)] | ||
Other(Vec<OsString>), | ||
} | ||
|
||
#[test] | ||
fn allow_invalid_utf8_subcommand_args_with_allow_external_subcommands() { | ||
let m = ExternalOs::try_parse_from(vec![ | ||
OsString::from(""), | ||
OsString::from("subcommand"), | ||
OsString::from("normal"), | ||
OsString::from_vec(vec![0xe9]), | ||
OsString::from("--another_normal"), | ||
]); | ||
assert_eq!( | ||
m.unwrap(), | ||
ExternalOs::Other(vec![ | ||
OsString::from("subcommand"), | ||
OsString::from("normal"), | ||
OsString::from_vec(vec![0xe9]), | ||
OsString::from("--another_normal"), | ||
]) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.