Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/uu/dd/src/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl SuffixType {
Self::Si => (SI_BASES, SI_SUFFIXES),
};
let mut i = 0;
while bases[i + 1] - bases[i] < n && i < suffixes.len() {
while i < suffixes.len() - 1 && bases[i + 1] - bases[i] < n {
i += 1;
}
(bases[i], suffixes[i])
Expand Down
2 changes: 1 addition & 1 deletion src/uu/df/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub(crate) fn to_magnitude_and_suffix(
let suffixes = suffix_type.suffixes();
let mut i = 0;

while bases[i + 1] - bases[i] < n && i < suffixes.len() {
while i < suffixes.len() - 1 && bases[i + 1] - bases[i] < n {
i += 1;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,25 @@ fn test_final_stats_three_char_limit() {
);
}

#[test]
fn test_final_stats_mb_suffix() {
// Exercise the suffix selection loop in to_magnitude_and_suffix through
// actual dd output, ensuring MB/MiB suffixes render without panic.
let result = new_ucmd!()
.args(&["bs=1000", "count=1000"])
.pipe_in("0".repeat(1_000_000))
.succeeds();
let s = result.stderr_str();
assert!(
s.contains("kB") || s.contains("MB"),
"expected SI suffix in output: {s}"
);
assert!(
s.contains("KiB") || s.contains("MiB"),
"expected IEC suffix in output: {s}"
);
}

#[test]
fn test_invalid_number_arg_gnu_compatibility() {
let commands = vec!["bs", "cbs", "count", "ibs", "obs", "seek", "skip"];
Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,34 @@ fn test_df_rounding() {
.stdout_does_not_match(&Regex::new("\\s\\d{3}\\.\\d[A-Z]").unwrap());
}

#[test]
fn test_df_human_readable_suffix() {
use regex::Regex;

// Exercise the suffix selection loop in to_magnitude_and_suffix through
// actual df output with -h (binary) and -H (SI), ensuring suffixes render
// without panic.
let re = Regex::new(r"\d[KMGTPEZY]").unwrap(); // spell-checker:disable-line

let output = new_ucmd!()
.args(&["-h", "--total"])
.succeeds()
.stdout_str_lossy();
assert!(
re.is_match(&output),
"expected human-readable suffix in -h output: {output}"
);

let output = new_ucmd!()
.args(&["-H", "--total"])
.succeeds()
.stdout_str_lossy();
assert!(
re.is_match(&output),
"expected human-readable suffix in -H output: {output}"
);
}

#[test]
fn test_df_output_overridden() {
let expected = if cfg!(target_os = "macos") {
Expand Down
Loading