Skip to content
Merged
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/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ fn format_verbose_difference(
} else {
// "{:>width$} {:>3o} {:>3o}"
let at_byte_str = at_byte_buf.format(at_byte);
let at_byte_padding = offset_width - at_byte_str.len();
let at_byte_padding = offset_width.saturating_sub(at_byte_str.len());

for _ in 0..at_byte_padding {
output.push(b' ')
Expand Down
20 changes: 20 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,26 @@ mod diff {
mod cmp {
use super::*;

// A file whose metadata length is 0 but which still yields bytes (/dev/zero)
// collapses the offset column to two characters, so the padding subtraction
// underflowed once the running offset reached three digits.
#[test]
#[cfg(unix)]
fn cmp_verbose_zero_length_metadata() -> Result<(), Box<dyn std::error::Error>> {
let mut file = NamedTempFile::new()?;
file.write_all(&[0xffu8; 150])?;

let mut cmd = cargo_bin_cmd!("diffutils");
cmd.arg("cmp")
.arg("--verbose")
.arg("/dev/zero")
.arg(file.path());
cmd.assert()
.code(predicate::eq(1))
.stdout(predicate::str::contains("150 0 377"));
Ok(())
}

#[test]
fn cmp_incompatible_params() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = cargo_bin_cmd!("diffutils");
Expand Down
Loading