Skip to content

Commit a82dce0

Browse files
committed
du: don't panic on block-size 0
1 parent e1645bb commit a82dce0

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/uu/du/src/du.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
683683
} else if matches.get_flag(options::BLOCK_SIZE_1M) {
684684
SizeFormat::BlockSize(1024 * 1024)
685685
} else {
686-
SizeFormat::BlockSize(read_block_size(
687-
matches
688-
.get_one::<String>(options::BLOCK_SIZE)
689-
.map(AsRef::as_ref),
690-
)?)
686+
let block_size_str = matches.get_one::<String>(options::BLOCK_SIZE);
687+
let block_size = read_block_size(block_size_str.map(AsRef::as_ref))?;
688+
if block_size == 0 {
689+
return Err(std::io::Error::other(format!(
690+
"invalid --{} argument {}",
691+
options::BLOCK_SIZE,
692+
block_size_str.map_or("???BUG", |v| v).quote()
693+
))
694+
.into());
695+
}
696+
SizeFormat::BlockSize(block_size)
691697
};
692698

693699
let traversal_options = TraversalOptions {

tests/by-util/test_du.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,3 +1248,19 @@ fn test_du_no_deduplicated_input_args() {
12481248
.collect();
12491249
assert_eq!(result_seq, ["2\td", "2\td", "2\td"]);
12501250
}
1251+
1252+
#[test]
1253+
fn test_du_blocksize_zero_do_not_panic() {
1254+
let ts = TestScenario::new(util_name!());
1255+
let at = &ts.fixtures;
1256+
at.write("foo", "some content");
1257+
for block_size in ["0", "00", "000", "0x0"] {
1258+
ts.ucmd()
1259+
.arg(format!("-B{block_size}"))
1260+
.arg("foo")
1261+
.fails()
1262+
.stderr_only(format!(
1263+
"du: invalid --block-size argument '{block_size}'\n"
1264+
));
1265+
}
1266+
}

0 commit comments

Comments
 (0)