mirror of
https://github.com/uutils/coreutils.git
synced 2025-12-23 08:47:37 +00:00
dirname: fix handling of non-UTF-8 filenames
This commit is contained in:
parent
c366551b02
commit
b301131a67
2 changed files with 26 additions and 4 deletions
|
|
@ -4,6 +4,7 @@
|
|||
// file that was distributed with this source code.
|
||||
|
||||
use clap::{Arg, ArgAction, Command};
|
||||
use std::ffi::OsString;
|
||||
use std::path::Path;
|
||||
use uucore::LocalizedCommand;
|
||||
use uucore::display::print_verbatim;
|
||||
|
|
@ -26,8 +27,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
|
||||
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
|
||||
|
||||
let dirnames: Vec<String> = matches
|
||||
.get_many::<String>(options::DIR)
|
||||
let dirnames: Vec<OsString> = matches
|
||||
.get_many::<OsString>(options::DIR)
|
||||
.unwrap_or_default()
|
||||
.cloned()
|
||||
.collect();
|
||||
|
|
@ -47,7 +48,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
}
|
||||
}
|
||||
None => {
|
||||
if p.is_absolute() || path == "/" {
|
||||
if p.is_absolute() || path.as_os_str() == "/" {
|
||||
print!("/");
|
||||
} else {
|
||||
print!(".");
|
||||
|
|
@ -79,6 +80,7 @@ pub fn uu_app() -> Command {
|
|||
Arg::new(options::DIR)
|
||||
.hide(true)
|
||||
.action(ArgAction::Append)
|
||||
.value_hint(clap::ValueHint::AnyPath),
|
||||
.value_hint(clap::ValueHint::AnyPath)
|
||||
.value_parser(clap::value_parser!(OsString)),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,3 +64,23 @@ fn test_pwd() {
|
|||
fn test_empty() {
|
||||
new_ucmd!().arg("").succeeds().stdout_is(".\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_dirname_non_utf8_paths() {
|
||||
use std::ffi::OsStr;
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
|
||||
// Create a test file with non-UTF-8 bytes in the name
|
||||
let non_utf8_bytes = b"test_\xFF\xFE/file.txt";
|
||||
let non_utf8_name = OsStr::from_bytes(non_utf8_bytes);
|
||||
|
||||
// Test that dirname handles non-UTF-8 paths without crashing
|
||||
let result = new_ucmd!().arg(non_utf8_name).succeeds();
|
||||
|
||||
// Just verify it didn't crash and produced some output
|
||||
// The exact output format may vary due to lossy conversion
|
||||
let output = result.stdout_str_lossy();
|
||||
assert!(!output.is_empty());
|
||||
assert!(output.contains("test_"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue