diff --git a/src/uu/pathchk/src/pathchk.rs b/src/uu/pathchk/src/pathchk.rs index 83897b34e4a..ba17083f0d6 100644 --- a/src/uu/pathchk/src/pathchk.rs +++ b/src/uu/pathchk/src/pathchk.rs @@ -127,6 +127,11 @@ pub fn uu_app() -> Command { /// check a path, given as a slice of it's components and an operating mode fn check_path(mode: &Mode, path: &[String]) -> bool { + // GNU rejects an empty file name in any portability mode before touching the filesystem. + if !matches!(mode, Mode::Default) && path.join("/").is_empty() { + show_error!("{}", translate!("pathchk-error-empty-file-name")); + return false; + } match *mode { Mode::Basic => check_basic(path), Mode::Extra => check_default(path) && check_extra(path), diff --git a/tests/by-util/test_pathchk.rs b/tests/by-util/test_pathchk.rs index 654585edc6f..b08098ca8cf 100644 --- a/tests/by-util/test_pathchk.rs +++ b/tests/by-util/test_pathchk.rs @@ -174,6 +174,23 @@ fn test_posix_all() { new_ucmd!().args(&["-p", "-P", ""]).fails().no_stdout(); } +#[test] +#[cfg(unix)] +fn test_empty_path_portability_message() { + // In every portability mode (-p, -P, --portability) GNU reports an empty + // file name with the program-name prefix and before any filesystem check. + for flag in ["-p", "-P", "--portability"] { + new_ucmd!() + .args(&[flag, ""]) + .fails() + .stderr_only("pathchk: empty file name\n"); + } + new_ucmd!() + .args(&["-p", "-P", ""]) + .fails() + .stderr_only("pathchk: empty file name\n"); +} + #[test] #[cfg(target_os = "linux")] fn test_pathchk_non_utf8_paths() {