diff --git a/src/uu/pathchk/src/pathchk.rs b/src/uu/pathchk/src/pathchk.rs index 02005f81b09..7efcdd4ca3a 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 { + // _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!( "{}", - 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..faee7fd4f75 100644 --- a/tests/by-util/test_pathchk.rs +++ b/tests/by-util/test_pathchk.rs @@ -71,6 +71,20 @@ fn test_posix_mode() { .fails() .no_stdout(); + // _POSIX_PATH_MAX (256) includes the terminating null byte, so 255 bytes + // is the longest portable file name + new_ucmd!() + .args(&["-p", &format!("{}a", "a/".repeat(127))]) + .succeeds() + .no_output(); + let path = format!("{}ab", "a/".repeat(127)); + new_ucmd!() + .args(&["-p", &path]) + .fails_with_code(1) + .stderr_only(format!( + "pathchk: limit 255 exceeded by length 256 of file name '{path}'\n" + )); + // fail on long filename new_ucmd!() .args(&[