Skip to content

Commit 6015c8f

Browse files
committed
df: treat env var with zero block size as invalid
1 parent 3ec3849 commit 6015c8f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

‎src/uu/df/src/blocks.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{env, fmt};
99

1010
use uucore::{
1111
display::Quotable,
12-
parser::parse_size::{ParseSizeError, parse_size_u64},
12+
parser::parse_size::{ParseSizeError, parse_size_non_zero_u64, parse_size_u64},
1313
};
1414

1515
/// The first ten powers of 1024.
@@ -213,7 +213,7 @@ pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSi
213213
fn block_size_from_env() -> Option<u64> {
214214
for env_var in ["DF_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
215215
if let Ok(env_size) = env::var(env_var) {
216-
return parse_size_u64(&env_size).ok();
216+
return parse_size_non_zero_u64(&env_size).ok();
217217
}
218218
}
219219

‎tests/by-util/test_df.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,24 @@ fn test_block_size_from_env() {
690690
assert_eq!(get_header("BLOCKSIZE", "333"), "333B-blocks");
691691
}
692692

693+
#[test]
694+
fn test_block_size_from_env_zero() {
695+
fn get_header(env_var: &str, env_value: &str) -> String {
696+
let output = new_ucmd!()
697+
.arg("--output=size")
698+
.env(env_var, env_value)
699+
.succeeds()
700+
.stdout_str_lossy();
701+
output.lines().next().unwrap().trim().to_string()
702+
}
703+
704+
let default_block_size_header = "1K-blocks";
705+
706+
assert_eq!(get_header("DF_BLOCK_SIZE", "0"), default_block_size_header);
707+
assert_eq!(get_header("BLOCK_SIZE", "0"), default_block_size_header);
708+
assert_eq!(get_header("BLOCKSIZE", "0"), default_block_size_header);
709+
}
710+
693711
#[test]
694712
fn test_block_size_from_env_precedences() {
695713
fn get_header(one: (&str, &str), two: (&str, &str)) -> String {
@@ -747,6 +765,16 @@ fn test_invalid_block_size_from_env() {
747765
let header = output.lines().next().unwrap().trim().to_string();
748766

749767
assert_eq!(header, default_block_size_header);
768+
769+
let output = new_ucmd!()
770+
.arg("--output=size")
771+
.env("DF_BLOCK_SIZE", "0")
772+
.env("BLOCK_SIZE", "222")
773+
.succeeds()
774+
.stdout_str_lossy();
775+
let header = output.lines().next().unwrap().trim().to_string();
776+
777+
assert_eq!(header, default_block_size_header);
750778
}
751779

752780
#[test]

0 commit comments

Comments
 (0)