Merge pull request #8932 from Ada-Armstrong/df_env_var_zero_block_size

df: treat env var with zero block size same as invalid
This commit is contained in:
Daniel Hofstetter
2025-10-17 16:16:48 +02:00
committed by GitHub
3 changed files with 39 additions and 2 deletions
+2 -2
View File
@@ -9,7 +9,7 @@ use std::{env, fmt};
use uucore::{
display::Quotable,
parser::parse_size::{ParseSizeError, parse_size_u64},
parser::parse_size::{ParseSizeError, parse_size_non_zero_u64, parse_size_u64},
};
/// The first ten powers of 1024.
@@ -213,7 +213,7 @@ pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSi
fn block_size_from_env() -> Option<u64> {
for env_var in ["DF_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
if let Ok(env_size) = env::var(env_var) {
return parse_size_u64(&env_size).ok();
return parse_size_non_zero_u64(&env_size).ok();
}
}
@@ -361,6 +361,15 @@ pub fn parse_size_u64(size: &str) -> Result<u64, ParseSizeError> {
Parser::default().parse_u64(size)
}
/// Same as `parse_size_u64()`, except 0 fails to parse
pub fn parse_size_non_zero_u64(size: &str) -> Result<u64, ParseSizeError> {
let v = Parser::default().parse_u64(size)?;
if v == 0 {
return Err(ParseSizeError::ParseFailure("0".to_string()));
}
Ok(v)
}
/// Same as `parse_size_u64()` - deprecated
#[deprecated = "Please use parse_size_u64(size: &str) -> Result<u64, ParseSizeError> OR parse_size_u128(size: &str) -> Result<u128, ParseSizeError> instead."]
pub fn parse_size(size: &str) -> Result<u64, ParseSizeError> {
+28
View File
@@ -690,6 +690,24 @@ fn test_block_size_from_env() {
assert_eq!(get_header("BLOCKSIZE", "333"), "333B-blocks");
}
#[test]
fn test_block_size_from_env_zero() {
fn get_header(env_var: &str, env_value: &str) -> String {
let output = new_ucmd!()
.arg("--output=size")
.env(env_var, env_value)
.succeeds()
.stdout_str_lossy();
output.lines().next().unwrap().trim().to_string()
}
let default_block_size_header = "1K-blocks";
assert_eq!(get_header("DF_BLOCK_SIZE", "0"), default_block_size_header);
assert_eq!(get_header("BLOCK_SIZE", "0"), default_block_size_header);
assert_eq!(get_header("BLOCKSIZE", "0"), default_block_size_header);
}
#[test]
fn test_block_size_from_env_precedences() {
fn get_header(one: (&str, &str), two: (&str, &str)) -> String {
@@ -747,6 +765,16 @@ fn test_invalid_block_size_from_env() {
let header = output.lines().next().unwrap().trim().to_string();
assert_eq!(header, default_block_size_header);
let output = new_ucmd!()
.arg("--output=size")
.env("DF_BLOCK_SIZE", "0")
.env("BLOCK_SIZE", "222")
.succeeds()
.stdout_str_lossy();
let header = output.lines().next().unwrap().trim().to_string();
assert_eq!(header, default_block_size_header);
}
#[test]