From 87bf73d341a23d0aae73734506e0b998721198b5 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:12:16 +0000 Subject: [PATCH 1/3] pathchk: reject a 256-byte file name with -p, like GNU _POSIX_PATH_MAX (256) counts the terminating null byte, so with -p the longest portable file name is 255 bytes. A 256-byte name was accepted, and a longer one was reported against limit 256 with the name unquoted. Report limit 255 and quote the name, as GNU pathchk does. --- src/uu/pathchk/src/pathchk.rs | 7 ++++--- tests/by-util/test_pathchk.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/uu/pathchk/src/pathchk.rs b/src/uu/pathchk/src/pathchk.rs index 02005f81b09..43ea4d6801a 100644 --- a/src/uu/pathchk/src/pathchk.rs +++ b/src/uu/pathchk/src/pathchk.rs @@ -145,11 +145,12 @@ fn check_path(mode: &Mode, path: &[String]) -> bool { fn check_basic(path: &[String]) -> bool { let joined_path = path.join("/"); let total_len = joined_path.len(); - // path length - if total_len > POSIX_PATH_MAX { + // path length: _POSIX_PATH_MAX counts the terminating null byte, so the + // longest portable file name is one byte shorter, as in GNU pathchk + if total_len >= POSIX_PATH_MAX { show_error!( "{}", - translate!("pathchk-error-posix-path-length-exceeded", "limit" => POSIX_PATH_MAX, "length" => total_len, "path" => joined_path) + translate!("pathchk-error-posix-path-length-exceeded", "limit" => POSIX_PATH_MAX - 1, "length" => total_len, "path" => joined_path.quote()) ); return false; } diff --git a/tests/by-util/test_pathchk.rs b/tests/by-util/test_pathchk.rs index 680db6c575e..c525733a5c8 100644 --- a/tests/by-util/test_pathchk.rs +++ b/tests/by-util/test_pathchk.rs @@ -84,6 +84,35 @@ fn test_posix_mode() { new_ucmd!().args(&["-p", "dir#/$file"]).fails().no_stdout(); } +#[test] +#[cfg(unix)] +fn test_posix_mode_path_max() { + // _POSIX_PATH_MAX (256) includes the terminating null byte, so 255 bytes + // is the longest portable file name. + let path_of_len = |len: usize| { + let mut path = "abcdefgh/".repeat(len / 9 + 1); + path.truncate(len); + if path.ends_with('/') { + path.pop(); + path.push('z'); + } + path + }; + + new_ucmd!() + .args(&["-p", &path_of_len(255)]) + .succeeds() + .no_output(); + + let path = path_of_len(256); + new_ucmd!() + .args(&["-p", &path]) + .fails_with_code(1) + .stderr_only(format!( + "pathchk: limit 255 exceeded by length 256 of file name '{path}'\n" + )); +} + #[test] #[cfg(unix)] fn test_posix_special() { From 1c3aa9624f8b51da01eb091327aa1e2a76a87112 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Mon, 9 Nov 2026 23:55:36 +0530 Subject: [PATCH 2/3] pathchk: apply review wording, fold boundary test into test_posix_mode --- src/uu/pathchk/src/pathchk.rs | 4 ++-- tests/by-util/test_pathchk.rs | 32 +++++++++++++------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/uu/pathchk/src/pathchk.rs b/src/uu/pathchk/src/pathchk.rs index 43ea4d6801a..7efcdd4ca3a 100644 --- a/src/uu/pathchk/src/pathchk.rs +++ b/src/uu/pathchk/src/pathchk.rs @@ -145,8 +145,8 @@ fn check_path(mode: &Mode, path: &[String]) -> bool { fn check_basic(path: &[String]) -> bool { let joined_path = path.join("/"); let total_len = joined_path.len(); - // path length: _POSIX_PATH_MAX counts the terminating null byte, so the - // longest portable file name is one byte shorter, as in GNU pathchk + // _POSIX_PATH_MAX counts the terminating null byte, so the + // longest portable file name is one byte shorter if total_len >= POSIX_PATH_MAX { show_error!( "{}", diff --git a/tests/by-util/test_pathchk.rs b/tests/by-util/test_pathchk.rs index c525733a5c8..9cdd2d02212 100644 --- a/tests/by-util/test_pathchk.rs +++ b/tests/by-util/test_pathchk.rs @@ -71,24 +71,8 @@ fn test_posix_mode() { .fails() .no_stdout(); - // fail on long filename - new_ucmd!() - .args(&[ - "-p", - format!("dir/{}", "file".repeat(libc::FILENAME_MAX as usize + 1)).as_str(), - ]) - .fails() - .no_stdout(); - - // fail on non-portable chars - new_ucmd!().args(&["-p", "dir#/$file"]).fails().no_stdout(); -} - -#[test] -#[cfg(unix)] -fn test_posix_mode_path_max() { // _POSIX_PATH_MAX (256) includes the terminating null byte, so 255 bytes - // is the longest portable file name. + // is the longest portable file name let path_of_len = |len: usize| { let mut path = "abcdefgh/".repeat(len / 9 + 1); path.truncate(len); @@ -98,12 +82,10 @@ fn test_posix_mode_path_max() { } path }; - new_ucmd!() .args(&["-p", &path_of_len(255)]) .succeeds() .no_output(); - let path = path_of_len(256); new_ucmd!() .args(&["-p", &path]) @@ -111,6 +93,18 @@ fn test_posix_mode_path_max() { .stderr_only(format!( "pathchk: limit 255 exceeded by length 256 of file name '{path}'\n" )); + + // fail on long filename + new_ucmd!() + .args(&[ + "-p", + format!("dir/{}", "file".repeat(libc::FILENAME_MAX as usize + 1)).as_str(), + ]) + .fails() + .no_stdout(); + + // fail on non-portable chars + new_ucmd!().args(&["-p", "dir#/$file"]).fails().no_stdout(); } #[test] From 2c8a7951d8a42b8772ba8790bac72c60a4cdb2f0 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Tue, 10 Nov 2026 01:21:43 +0530 Subject: [PATCH 3/3] pathchk: simplify the 255/256-byte path test --- tests/by-util/test_pathchk.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/tests/by-util/test_pathchk.rs b/tests/by-util/test_pathchk.rs index 9cdd2d02212..faee7fd4f75 100644 --- a/tests/by-util/test_pathchk.rs +++ b/tests/by-util/test_pathchk.rs @@ -73,20 +73,11 @@ fn test_posix_mode() { // _POSIX_PATH_MAX (256) includes the terminating null byte, so 255 bytes // is the longest portable file name - let path_of_len = |len: usize| { - let mut path = "abcdefgh/".repeat(len / 9 + 1); - path.truncate(len); - if path.ends_with('/') { - path.pop(); - path.push('z'); - } - path - }; - new_ucmd!() - .args(&["-p", &path_of_len(255)]) + new_ucmd!() + .args(&["-p", &format!("{}a", "a/".repeat(127))]) .succeeds() .no_output(); - let path = path_of_len(256); + let path = format!("{}ab", "a/".repeat(127)); new_ucmd!() .args(&["-p", &path]) .fails_with_code(1)