ls: fix zero block size handling to match GNU ls

- Reject --block-size=0 with "invalid --block-size argument '0'" error
  using parse_size_non_zero_u64
- Add test coverage for both command-line and env var cases

Matches GNU ls behavior where command-line zero is invalid but
environment variable zero is silently ignored.
This commit is contained in:
Zackary Ayoun 2025-10-16 17:02:26 +00:00
parent db9c50c327
commit 3ff51d6402
2 changed files with 16 additions and 2 deletions

View file

@ -68,7 +68,7 @@ use uucore::{
line_ending::LineEnding,
os_str_as_bytes_lossy,
parser::parse_glob,
parser::parse_size::parse_size_u64,
parser::parse_size::parse_size_non_zero_u64,
parser::shortcut_value_parser::ShortcutValueParser,
quoting_style::{QuotingStyle, locale_aware_escape_dir_name, locale_aware_escape_name},
show, show_error, show_warning,
@ -902,7 +902,7 @@ impl Config {
let (file_size_block_size, block_size) = if !opt_si && !opt_hr && !raw_block_size.is_empty()
{
match parse_size_u64(&raw_block_size.to_string_lossy()) {
match parse_size_non_zero_u64(&raw_block_size.to_string_lossy()) {
Ok(size) => match (is_env_var_blocksize, opt_kb) {
(true, true) => (DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE),
(true, false) => (DEFAULT_FILE_SIZE_BLOCK_SIZE, size),

View file

@ -5355,6 +5355,12 @@ fn test_ls_invalid_block_size() {
.fails_with_code(2)
.no_stdout()
.stderr_is("ls: invalid --block-size argument 'invalid'\n");
new_ucmd!()
.arg("--block-size=0")
.fails_with_code(2)
.no_stdout()
.stderr_is("ls: invalid --block-size argument '0'\n");
}
#[cfg(all(unix, feature = "dd"))]
@ -5394,6 +5400,14 @@ fn test_ls_invalid_block_size_in_env_var() {
.succeeds()
.stdout_contains_line("total 4")
.stdout_contains(" 1024 ");
scene
.ucmd()
.arg("-og")
.env("BLOCKSIZE", "0")
.succeeds()
.stdout_contains_line("total 4")
.stdout_contains(" 1024 ");
}
#[cfg(all(unix, feature = "dd"))]