diff --git a/src/options/parser.rs b/src/options/parser.rs index 5e8ba7da7..c55617e3c 100644 --- a/src/options/parser.rs +++ b/src/options/parser.rs @@ -316,7 +316,14 @@ impl clap::builder::TypedValueParser for TimeFormatParser { _arg: Option<&clap::Arg>, value: &std::ffi::OsStr, ) -> Result { - match TimeFormat::try_from_str(value.to_str().unwrap()) { + let Some(value) = value.to_str() else { + return Err(Error::raw( + clap::error::ErrorKind::InvalidUtf8, + "invalid UTF-8 in --time-style value\n", + ) + .with_cmd(cmd)); + }; + match TimeFormat::try_from_str(value) { Err(s) => Err(Error::raw(clap::error::ErrorKind::InvalidValue, s).with_cmd(cmd)), Ok(v) => Ok(v), } @@ -368,4 +375,14 @@ pub mod test { ["file1", "file2"] ); } + + #[cfg(unix)] + #[test] + fn time_style_non_utf8_is_a_usage_error() { + use std::os::unix::ffi::OsStringExt; + // A non-UTF-8 --time-style value should be a usage error, not a panic. + let value = OsString::from_vec(vec![0xff, 0xfe]); + let result = mock_cli_try(vec![OsString::from("--time-style"), value]); + assert!(result.is_err()); + } } diff --git a/tests/ptests/ptest_a82ad7ec2e961f84.stdout b/tests/ptests/ptest_a82ad7ec2e961f84.stdout index ca3b7e3a0..a0354c866 100644 --- a/tests/ptests/ptest_a82ad7ec2e961f84.stdout +++ b/tests/ptests/ptest_a82ad7ec2e961f84.stdout @@ -1,4 +1,4 @@ eza eza - A modern, maintained replacement for ls -v0.23.4 [+git] (pre-release debug build!) +v0.23.5 [+git] (pre-release debug build!) https://github.com/eza-community/eza diff --git a/tests/ptests/ptest_af29d370729835d8.stdout b/tests/ptests/ptest_af29d370729835d8.stdout index ca3b7e3a0..a0354c866 100644 --- a/tests/ptests/ptest_af29d370729835d8.stdout +++ b/tests/ptests/ptest_af29d370729835d8.stdout @@ -1,4 +1,4 @@ eza eza - A modern, maintained replacement for ls -v0.23.4 [+git] (pre-release debug build!) +v0.23.5 [+git] (pre-release debug build!) https://github.com/eza-community/eza