Skip to content

Commit 529f011

Browse files
cakebakersylvestre
authored andcommitted
date: allow non-utf8 path for --file
1 parent 00a0a60 commit 529f011

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/uu/date/src/date.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
350350
USimpleError::new(1, format!("invalid date '{escaped_str}'"))
351351
})?;
352352
DateSource::Human(date.into())
353-
} else if let Some(file) = matches.get_one::<String>(OPT_FILE) {
354-
match file.as_ref() {
355-
"-" => DateSource::Stdin,
353+
} else if let Some(file) = matches.get_one::<OsString>(OPT_FILE) {
354+
match file.as_encoded_bytes() {
355+
b"-" => DateSource::Stdin,
356356
_ => DateSource::File(file.into()),
357357
}
358358
} else if let Some(file) = matches.get_one::<OsString>(OPT_REFERENCE) {
@@ -695,6 +695,7 @@ pub fn uu_app() -> Command {
695695
.long(OPT_FILE)
696696
.value_name("DATEFILE")
697697
.value_hint(clap::ValueHint::FilePath)
698+
.value_parser(clap::value_parser!(OsString))
698699
.conflicts_with(OPT_DATE)
699700
.help(translate!("date-help-file")),
700701
)

tests/by-util/test_date.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,20 @@ fn test_date_for_empty_file() {
642642
ucmd.arg("--file").arg(file).succeeds().no_output();
643643
}
644644

645+
#[test]
646+
#[cfg(target_os = "linux")]
647+
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
648+
fn test_date_for_file_with_non_utf8_path() {
649+
use std::os::unix::ffi::OsStrExt;
650+
651+
let (at, mut ucmd) = at_and_ucmd!();
652+
653+
let file = std::ffi::OsStr::from_bytes(b"file_\xFF\xFE.txt");
654+
std::fs::File::create(at.plus(file)).unwrap();
655+
656+
ucmd.arg("--file").arg(file).succeeds().no_output();
657+
}
658+
645659
#[test]
646660
fn test_date_file_invalid_utf8_line() {
647661
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)