From 4f1c66e07910d5a9722dc34c11bfeb4dada34874 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Wed, 12 Aug 2026 23:41:50 -0300 Subject: [PATCH 01/17] build(deps): add test-case as a dev-dependency The side-by-side layout tests need parameterized cases so that each width and tab size shows up as its own named test instead of a loop that reports a single failure with no indication of which input broke. --- Cargo.lock | 34 ++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + 2 files changed, 35 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index b2fa9e8..91fcb2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -273,6 +273,7 @@ dependencies = [ "regex", "same-file", "tempfile", + "test-case", "unicode-width", ] @@ -759,6 +760,39 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683" +[[package]] +name = "test-case" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb2550dd13afcd286853192af8601920d959b14c401fcece38071d53bf0768a8" +dependencies = [ + "test-case-macros", +] + +[[package]] +name = "test-case-core" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcb7fd841cd518e279be3d5a3eb0636409487998a4aff22f3de87b81e88384f" +dependencies = [ + "cfg-if", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "test-case-macros" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c89e72a01ed4c579669add59014b9a524d609c0c88c6a585ce37485879f6ffb" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "test-case-core", +] + [[package]] name = "toml_datetime" version = "1.1.2+spec-1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 719a311..48132ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ pretty_assertions = "1.4.0" predicates = "3.1.0" rand = "0.10.0" tempfile = "3.26.0" +test-case = "3.3.1" [profile.release] lto = "thin" From 94a4d38cb6a4e07f226e8fec3417ab48765c8e3c Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Wed, 12 Aug 2026 23:43:19 -0300 Subject: [PATCH 02/17] fix(side-diff): compute the column layout without signed casts `Config::new` sized the side-by-side columns with signed arithmetic on the `--tabsize` value, which `params.rs` accepts with no upper bound. `tab_size + GUTTER_WIDTH_MIN` overflowed for a large one: with `-C overflow-checks=on` that aborted, and in release it wrapped and drew bogus widths. A value above `isize::MAX` did not even abort, it cast to a negative number and silently laid the columns out as if the tab size were small. Closes #264. The calculation moves into `Config::layout`, in `usize` throughout, and holds for every input on its own rather than relying on validation upstream: - An early return for `tab_size > full_width`. This is exact, not a clamp: the offset is a multiple of `tab_size`, so it is either zero or already past the right edge, and neither leaves room for a second column. It is also what keeps the sum below in range. - `saturating_add` for the gutter sum, which covers the one case the strict `>` lets through, `full_width == tab_size == usize::MAX`. - `saturating_sub` for the two bounds that could go negative. `full_width = 5, tab_size = 8` really does produce an offset past the right edge. - `usize::midpoint` for the balance point, which cannot overflow. - `saturating_sub` on `separator_pos`, which underflowed for a zero width. Unreachable from the CLI, reachable through the library. A tab stop every zero columns has no meaning and every reader of `tab_size` divides by it, so `Config::new` now normalizes it to one. The layout alone was not enough: fixing it only moved the division by zero from `layout` to `format_tabs_and_spaces`. The new `mod layout` covers the boundaries, including the two that pin the design down: `full_width = 10, tab_size = 8` still leaves two columns, so no looser guard is correct, and `tab_size == full_width` yields a non-zero offset with an empty half line. --- src/side_diff.rs | 191 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 171 insertions(+), 20 deletions(-) diff --git a/src/side_diff.rs b/src/side_diff.rs index 1128e2e..921213d 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -3,7 +3,7 @@ // For the full copyright and license information, please view the LICENSE-* // files that was distributed with this source code. -use core::cmp::{max, min}; +use core::cmp::min; use diff::Result; use std::{io::Write, vec}; use unicode_width::UnicodeWidthStr; @@ -74,26 +74,49 @@ impl<'a> Iterator for CharIter<'a> { impl Config { pub fn new(full_width: usize, tab_size: usize, expanded: bool) -> Self { - // diff uses this calculation to calculate the size of a half line - // based on the options passed (like -w, -t, etc.). It's actually - // pretty useless, because we (actually) don't have any size modifiers - // that can change this, however I just want to leave the calculate - // here, since it's not very clear and may cause some confusion - - let w = full_width as isize; - let t = tab_size as isize; - let t_plus_g = t + GUTTER_WIDTH_MIN as isize; - let unaligned_off = (w >> 1) + (t_plus_g >> 1) + (w & t_plus_g & 1); - let off = unaligned_off - unaligned_off % t; - let hw = max(0, min(off - GUTTER_WIDTH_MIN as isize, w - off)) as usize; - let c2o = if hw != 0 { off as usize } else { w as usize }; + let tab_size = tab_size.max(1); + + let (half_width, column_two_offset) = Self::layout(full_width, tab_size); Self { expanded, - sdiff_column_two_offset: c2o, + sdiff_column_two_offset: column_two_offset, tab_size, - sdiff_half_width: hw, - separator_pos: ((hw + c2o - 1) >> 1), + sdiff_half_width: half_width, + separator_pos: (half_width + column_two_offset).saturating_sub(1) >> 1, + } + } + + fn layout(full_width: usize, tab_size: usize) -> (usize, usize) { + debug_assert!(tab_size != 0); + + // The offset is a multiple of tab_size, so a tab stop wider than the + // whole line leaves only two possibilities, zero, or past the right + // edge. Neither leaves room for a second column, so the layout + // collapses to one + if tab_size > full_width { + return (0, full_width); + } + + // Column two starts at the tab stop nearest the midpoint of the two + // halves. midpoint instead of (full_width + span) / 2 because that + // sum does not fit for a width near usize::MAX. The saturation covers + // a value within GUTTER_WIDTH_MIN of usize::MAX on a line of equal width. + let span = tab_size.saturating_add(GUTTER_WIDTH_MIN); + let balance = full_width.midpoint(span); + let offset = balance - balance % tab_size; + + // If either bound would go negative, the half line does not fit. + let half_width = min( + offset.saturating_sub(GUTTER_WIDTH_MIN), + full_width.saturating_sub(offset), + ); + + // If it does not fit, one column spans the whole line. + if half_width == 0 { + (0, full_width) + } else { + (half_width, offset) } } } @@ -223,7 +246,6 @@ fn process_half_line( } } - // gnu sdiff do not tabulate the hole empty right line, instead, just keep the line empty if !is_right { // we always sum + 1 or + GUTTER_WIDTH_MIN cause we want to expand // up to the third column of the gutter column if the gutter is gutter white space, @@ -314,8 +336,6 @@ pub fn diff( output: &mut T, params: &Params, ) -> Vec { - // ^ The left file ^ The right file - let mut left_lines: Vec<&[u8]> = from_file.split_inclusive(|&c| c == b'\n').collect(); let mut right_lines: Vec<&[u8]> = to_file.split_inclusive(|&c| c == b'\n').collect(); let config = Config::new(params.width, params.tabsize, params.expand_tabs); @@ -369,6 +389,136 @@ mod tests { use super::*; + mod layout { + use super::*; + use test_case::test_case; + + #[track_caller] + fn assert_layout( + full_width: usize, + tab_size: usize, + half_width: usize, + column_two_offset: usize, + separator_pos: usize, + ) { + let config = Config::new(full_width, tab_size, false); + + assert_eq!(config.sdiff_half_width, half_width, "half width"); + assert_eq!( + config.sdiff_column_two_offset, column_two_offset, + "column two offset" + ); + assert_eq!(config.separator_pos, separator_pos, "separator pos"); + } + + #[test] + fn default_width_and_tab_size() { + assert_layout(130, 8, 61, 64, 62); + } + + #[test] + fn common_widths() { + assert_layout(80, 8, 37, 40, 38); + assert_layout(10, 7, 3, 7, 4); + } + + #[test] + fn tab_size_wider_than_the_gutter_still_leaves_two_columns() { + assert_layout(10, 8, 2, 8, 4); + } + + #[test] + fn tab_size_equal_to_the_width_leaves_one_column() { + assert_layout(10, 10, 0, 10, 4); + } + + #[test] + fn tab_size_wider_than_the_width_leaves_one_column() { + assert_layout(10, 11, 0, 10, 4); + } + + #[test] + fn smallest_width_the_cli_accepts() { + assert_layout(1, 8, 0, 1, 0); + } + + #[test] + fn column_two_offset_past_the_right_edge() { + assert_layout(5, 8, 0, 5, 2); + } + + #[test] + fn huge_tab_size() { + assert_layout(130, usize::MAX, 0, 130, 64); + assert_layout(130, usize::MAX / 2, 0, 130, 64); + assert_layout(130, 9223372036854775805, 0, 130, 64); + } + + #[test] + fn huge_width() { + let half = usize::MAX / 2 - 2; + let offset = usize::MAX / 2 + 1; + assert_layout(usize::MAX, 8, half, offset, (half + offset - 1) >> 1); + } + + #[test] + fn huge_width_and_tab_size() { + assert_layout(usize::MAX, usize::MAX, 0, usize::MAX, usize::MAX >> 1); + } + + #[test] + fn zero_width() { + assert_layout(0, 8, 0, 0, 0); + } + + #[test] + fn zero_tab_size_behaves_like_one() { + assert_layout(130, 0, 63, 67, 64); + assert_layout(130, 1, 63, 67, 64); + } + + #[track_caller] + fn assert_renders(from: &[u8], to: &[u8], width: usize, tabsize: usize) { + for expand_tabs in [false, true] { + let params = Params { + width, + tabsize, + expand_tabs, + ..Default::default() + }; + let mut output = vec![]; + + diff(from, to, &mut output, ¶ms); + } + } + + #[test_case(0 ; "zero")] + #[test_case(1 ; "one")] + #[test_case(2 ; "two")] + #[test_case(7 ; "odd")] + #[test_case(8 ; "default")] + #[test_case(1000 ; "wider than any line")] + #[test_case(usize::MAX / 2 ; "half of usize")] + #[test_case(usize::MAX - 1 ; "one below usize max")] + #[test_case(usize::MAX ; "usize max")] + fn extreme_tab_size_renders(tabsize: usize) { + for width in [0, 1, 2, 3, 5, 10, 40, 130, 1000, 65535] { + assert_renders(b"a\tb\n", b"a\tc\n", width, tabsize); + } + } + + #[test_case(b"aaa\tbbb\n", b"aaa\tccc\n" ; "tabs on both sides")] + #[test_case(b"\t\t\n", b"\n" ; "tabs against an empty line")] + #[test_case("\u{4f60}\u{597d}\t\u{1f600}\n".as_bytes(), b"a\n" ; "wide and multibyte")] + fn every_small_width_and_tab_size_renders(from: &[u8], to: &[u8]) { + for width in 0..96 { + for tabsize in 0..96 { + assert_renders(from, to, width, tabsize); + } + } + } + } + mod format_tabs_and_spaces { use super::*; @@ -1236,6 +1386,7 @@ mod tests { #[test] fn test_full_width_40_tab_8() { + // Expanded, so the layout uses a tab stop on every column. let config = create_config(40, 8, true); assert_eq!(config.sdiff_half_width, 16); assert_eq!(config.sdiff_column_two_offset, 24); From 8a174b1242bd0124e1e68e12783c60680caf7914 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Wed, 12 Aug 2026 23:43:52 -0300 Subject: [PATCH 03/17] fix(side-diff): honour --expand-tabs in the column layout `Config::new` took `expanded` but ignored it when sizing the columns. The manual's "Preserving Tab Stop Alignment" section explains why it matters: column two has to start on a tab stop only so that tabs in the right column keep their position relative to the stops. With `--expand-tabs` there are no tabs left in the output, so there is no grid to preserve and every column is a stop. Compared against the output of `diff` from GNU diffutils 3.10. The separator column now matches it for `-y -t` at widths 40, 80 and 130, and for `-y -t --tabsize=4 --width=100`: columns 19, 39, 64 and 49. At the default width it sat on column 62 before, two columns off. The tab size still decides how far an expanded tab reaches, so it stays in the field and only `layout` sees the 1. Feeding the 1 into the field would shrink every expanded tab to a single space. The widened half line makes a crash reachable. A tab size near `usize::MAX` used to yield an empty half line, so `process_half_line` returned before drawing anything; with `-t` the line now fits and three expressions of the form `current_width + tab_size - (current_width % tab_size)` overflowed. `format_tabs_and_spaces` and the tab arm now measure the step to the next stop against the room that is left instead of summing absolute columns, which the surrounding `current_width <= max_width` already bounds. `test_full_width_40_tab_8` used `expanded = true` and expected the widths computed while ignoring it. The separator lands on column 19 either way, so only the half width and the offset change. The new tests were checked by mutation. Dropping the ternary breaks `expanded_tabs_widen_the_half_line`, `expanded_tabs_lay_out_as_a_stop_on_every_column` and `test_full_width_40_tab_8`; applying it to the field instead breaks `expanded_tabs_keep_the_real_tab_size_for_rendering` and `expanded_tabs_reach_the_next_real_tab_stop`. --- src/side_diff.rs | 135 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 117 insertions(+), 18 deletions(-) diff --git a/src/side_diff.rs b/src/side_diff.rs index 921213d..85fd21d 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -76,7 +76,8 @@ impl Config { pub fn new(full_width: usize, tab_size: usize, expanded: bool) -> Self { let tab_size = tab_size.max(1); - let (half_width, column_two_offset) = Self::layout(full_width, tab_size); + let (half_width, column_two_offset) = + Self::layout(full_width, if expanded { 1 } else { tab_size }); Self { expanded, @@ -143,10 +144,15 @@ fn format_tabs_and_spaces( return Ok(()); } - while current + (tab_size - current % tab_size) <= to { - let next_tab = current + (tab_size - current % tab_size); + loop { + let advance = tab_size - current % tab_size; + + if advance > to - current { + break; + } + buf.write_all(b"\t")?; - current = next_tab; + current += advance; } while current < to { @@ -215,17 +221,18 @@ fn process_half_line( match char { b"\t" => { - if expanded && (current_width + tab_size - (current_width % tab_size)) <= max_width - { - let mut spaces = tab_size - (current_width % tab_size); - while spaces > 0 { - buf.write_all(b" ")?; - current_width += 1; - spaces -= 1; + let advance = tab_size - current_width % tab_size; + + if advance <= max_width - current_width { + if expanded { + for _ in 0..advance { + buf.write_all(b" ")?; + } + } else { + buf.write_all(b"\t")?; } - } else if current_width + tab_size - (current_width % tab_size) <= max_width { - buf.write_all(b"\t")?; - current_width += tab_size - (current_width % tab_size); + + current_width += advance; } } b"\n" => { @@ -401,7 +408,44 @@ mod tests { column_two_offset: usize, separator_pos: usize, ) { - let config = Config::new(full_width, tab_size, false); + assert_config( + full_width, + tab_size, + false, + half_width, + column_two_offset, + separator_pos, + ); + } + + #[track_caller] + fn assert_layout_expanded( + full_width: usize, + tab_size: usize, + half_width: usize, + column_two_offset: usize, + separator_pos: usize, + ) { + assert_config( + full_width, + tab_size, + true, + half_width, + column_two_offset, + separator_pos, + ); + } + + #[track_caller] + fn assert_config( + full_width: usize, + tab_size: usize, + expanded: bool, + half_width: usize, + column_two_offset: usize, + separator_pos: usize, + ) { + let config = Config::new(full_width, tab_size, expanded); assert_eq!(config.sdiff_half_width, half_width, "half width"); assert_eq!( @@ -416,6 +460,61 @@ mod tests { assert_layout(130, 8, 61, 64, 62); } + #[test] + fn expanded_tabs_lay_out_as_a_stop_on_every_column() { + assert_layout(130, 1, 63, 67, 64); + assert_layout_expanded(130, 8, 63, 67, 64); + assert_layout_expanded(130, usize::MAX, 63, 67, 64); + } + + #[test] + fn expanded_tabs_widen_the_half_line() { + assert_layout(130, 8, 61, 64, 62); + assert_layout_expanded(130, 8, 63, 67, 64); + + assert_layout(40, 8, 16, 24, 19); + assert_layout_expanded(40, 8, 18, 22, 19); + } + + #[test] + fn expanded_tabs_keep_the_real_tab_size_for_rendering() { + assert_eq!(Config::new(130, 8, true).tab_size, 8); + assert_eq!(Config::new(130, 8, false).tab_size, 8); + } + + #[test] + fn expanded_tabs_reach_the_next_real_tab_stop() { + let params = Params { + width: 40, + tabsize: 8, + expand_tabs: true, + ..Default::default() + }; + let mut output = vec![]; + + diff(b"a\tb\n", b"a\tc\n", &mut output, ¶ms); + + assert!(!output.contains(&b'\t'), "expanded output still has tabs"); + assert!( + output.starts_with(b"a b"), + "tab did not reach column 8" + ); + } + + #[test] + fn unexpanded_tabs_stay_tabs() { + let params = Params { + width: 40, + tabsize: 8, + ..Default::default() + }; + let mut output = vec![]; + + diff(b"a\tb\n", b"a\tc\n", &mut output, ¶ms); + + assert!(output.starts_with(b"a\tb")); + } + #[test] fn common_widths() { assert_layout(80, 8, 37, 40, 38); @@ -1388,9 +1487,9 @@ mod tests { fn test_full_width_40_tab_8() { // Expanded, so the layout uses a tab stop on every column. let config = create_config(40, 8, true); - assert_eq!(config.sdiff_half_width, 16); - assert_eq!(config.sdiff_column_two_offset, 24); - assert_eq!(config.separator_pos, 19); // (16 +24 -1) /2 = 19.5 + assert_eq!(config.sdiff_half_width, 18); + assert_eq!(config.sdiff_column_two_offset, 22); + assert_eq!(config.separator_pos, 19); // (18 + 22 - 1) / 2 = 19.5 } #[test] From 616624293e99006b5efe36aaad6411ef48e4108c Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Wed, 12 Aug 2026 23:44:09 -0300 Subject: [PATCH 04/17] test(fuzz): fuzz width and tabsize in the side-by-side target `width` and `tabsize` were commented out of `fuzz_side`, along with a `width == 0 || tabsize == 0` early return that was commented out too, because the column arithmetic could not take arbitrary values. It can now, so both are fed to the target and neither guard is needed. They are `u16` rather than `usize`: the layout handles the whole range, but a width near `usize::MAX` asks the renderer for petabytes of padding, which would only produce timeouts. --- fuzz/fuzz_targets/fuzz_side.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/fuzz/fuzz_targets/fuzz_side.rs b/fuzz/fuzz_targets/fuzz_side.rs index 45580ef..95c4cee 100644 --- a/fuzz/fuzz_targets/fuzz_side.rs +++ b/fuzz/fuzz_targets/fuzz_side.rs @@ -8,16 +8,12 @@ use diffutilslib::params::Params; use std::fs::{self, File}; use std::io::Write; -fuzz_target!(|x: (Vec, Vec, /* usize, usize */ bool)| { - let (original, new, /* width, tabsize, */ expand) = x; - - // if width == 0 || tabsize == 0 { - // return; - // } +fuzz_target!(|x: (Vec, Vec, u16, u16, bool)| { + let (original, new, width, tabsize, expand) = x; let params = Params { - // width, - // tabsize, + width: width as usize, + tabsize: tabsize as usize, expand_tabs: expand, ..Default::default() }; From 93a7ac1487adf21042d85b9d43aea91e4a15afba Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Thu, 13 Aug 2026 00:31:05 -0300 Subject: [PATCH 05/17] test(side-diff): drop the comment on the expanded layout expectation The assertions state the widths already, and the surrounding test name carries the tab size. --- src/side_diff.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/side_diff.rs b/src/side_diff.rs index 85fd21d..a57ffb4 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -1485,7 +1485,6 @@ mod tests { #[test] fn test_full_width_40_tab_8() { - // Expanded, so the layout uses a tab stop on every column. let config = create_config(40, 8, true); assert_eq!(config.sdiff_half_width, 18); assert_eq!(config.sdiff_column_two_offset, 22); From ed163a4d04af8a61584cfccde11086346bc23d87 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Thu, 13 Aug 2026 00:43:18 -0300 Subject: [PATCH 06/17] test(side-diff): cover the carriage return padding at the widest line A carriage return pads all the way to column two, the only caller that reaches the far end of the line. That walk used to add absolute columns and overflowed with `--width` and `--tabsize` at the top of the range, which aborts under `-C overflow-checks=on`. Both values sit at the maximum on purpose: a smaller tab size walks to that end one stop at a time, and a smaller width never reaches the sum that overflowed. Expansion stays off, since it pads with spaces one column at a time and would not finish at this width. --- src/side_diff.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/side_diff.rs b/src/side_diff.rs index a57ffb4..ffbdb15 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -606,6 +606,19 @@ mod tests { } } + #[test] + fn carriage_return_at_the_widest_line_and_tab_size() { + let params = Params { + width: usize::MAX, + tabsize: usize::MAX, + expand_tabs: false, + ..Default::default() + }; + let mut output = vec![]; + + diff(b"a\rb\n", b"c\n", &mut output, ¶ms); + } + #[test_case(b"aaa\tbbb\n", b"aaa\tccc\n" ; "tabs on both sides")] #[test_case(b"\t\t\n", b"\n" ; "tabs against an empty line")] #[test_case("\u{4f60}\u{597d}\t\u{1f600}\n".as_bytes(), b"a\n" ; "wide and multibyte")] From 12feb6a8a6c3e3b1740825cfd2b14b9eda317a49 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Thu, 13 Aug 2026 00:53:07 -0300 Subject: [PATCH 07/17] test(fuzz): fuzz the whole tab size range in the side-by-side target A tab size wider than the line collapses the layout to a single column and draws nothing, so the whole `usize` range costs no more than a small one. Capping it at `u16` left out the values above `isize::MAX`, which are the ones the old signed arithmetic turned negative. The width stays a `u16`, since the padding it asks for is written one column at a time and the cost scales with it. --- fuzz/fuzz_targets/fuzz_side.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/fuzz/fuzz_targets/fuzz_side.rs b/fuzz/fuzz_targets/fuzz_side.rs index 95c4cee..69d62aa 100644 --- a/fuzz/fuzz_targets/fuzz_side.rs +++ b/fuzz/fuzz_targets/fuzz_side.rs @@ -8,12 +8,17 @@ use diffutilslib::params::Params; use std::fs::{self, File}; use std::io::Write; -fuzz_target!(|x: (Vec, Vec, u16, u16, bool)| { +// We can't fuzz with width equals to usize, otherwise we +// would could have 2⁶⁴ - 1 of padding columns, which means +// exabytes nescessary for this. u32 also doesn't have a +// great perfomance here, with almost 537 MB being nescessary +// and 57 seconds of execution. +fuzz_target!(|x: (Vec, Vec, u16, usize, bool)| { let (original, new, width, tabsize, expand) = x; let params = Params { width: width as usize, - tabsize: tabsize as usize, + tabsize, expand_tabs: expand, ..Default::default() }; From 88c35c8285a7eb5d2d80bccc51dc55620b02cf76 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Thu, 13 Aug 2026 00:54:49 -0300 Subject: [PATCH 08/17] style(fuzz): write the exponent in the width comment as ASCII Superscript digits do not survive every terminal or editor the file gets read in. --- fuzz/fuzz_targets/fuzz_side.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuzz/fuzz_targets/fuzz_side.rs b/fuzz/fuzz_targets/fuzz_side.rs index 69d62aa..3fddb94 100644 --- a/fuzz/fuzz_targets/fuzz_side.rs +++ b/fuzz/fuzz_targets/fuzz_side.rs @@ -9,7 +9,7 @@ use std::fs::{self, File}; use std::io::Write; // We can't fuzz with width equals to usize, otherwise we -// would could have 2⁶⁴ - 1 of padding columns, which means +// would could have 2^64 - 1 of padding columns, which means // exabytes nescessary for this. u32 also doesn't have a // great perfomance here, with almost 537 MB being nescessary // and 57 seconds of execution. From bd0a9bcb5c518d8f0942cf1deae7233b3c93626d Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Mon, 5 Oct 2026 22:10:25 -0300 Subject: [PATCH 09/17] test(side-diff): replace test-case with plain loops Drop the test-case dev-dependency. Only two layout tests used it, so `extreme_tab_size_renders` and `every_small_width_and_tab_size_renders` now iterate over the same inputs in a loop inside a single `#[test]`. The inputs are unchanged. The cases no longer show up as separately named tests. --- Cargo.lock | 34 ---------------------------------- Cargo.toml | 1 - src/side_diff.rs | 48 ++++++++++++++++++++++++++++-------------------- 3 files changed, 28 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91fcb2d..b2fa9e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -273,7 +273,6 @@ dependencies = [ "regex", "same-file", "tempfile", - "test-case", "unicode-width", ] @@ -760,39 +759,6 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683" -[[package]] -name = "test-case" -version = "3.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb2550dd13afcd286853192af8601920d959b14c401fcece38071d53bf0768a8" -dependencies = [ - "test-case-macros", -] - -[[package]] -name = "test-case-core" -version = "3.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adcb7fd841cd518e279be3d5a3eb0636409487998a4aff22f3de87b81e88384f" -dependencies = [ - "cfg-if", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "test-case-macros" -version = "3.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c89e72a01ed4c579669add59014b9a524d609c0c88c6a585ce37485879f6ffb" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "test-case-core", -] - [[package]] name = "toml_datetime" version = "1.1.2+spec-1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 48132ca..719a311 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,6 @@ pretty_assertions = "1.4.0" predicates = "3.1.0" rand = "0.10.0" tempfile = "3.26.0" -test-case = "3.3.1" [profile.release] lto = "thin" diff --git a/src/side_diff.rs b/src/side_diff.rs index ffbdb15..4654797 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -398,7 +398,6 @@ mod tests { mod layout { use super::*; - use test_case::test_case; #[track_caller] fn assert_layout( @@ -591,18 +590,22 @@ mod tests { } } - #[test_case(0 ; "zero")] - #[test_case(1 ; "one")] - #[test_case(2 ; "two")] - #[test_case(7 ; "odd")] - #[test_case(8 ; "default")] - #[test_case(1000 ; "wider than any line")] - #[test_case(usize::MAX / 2 ; "half of usize")] - #[test_case(usize::MAX - 1 ; "one below usize max")] - #[test_case(usize::MAX ; "usize max")] - fn extreme_tab_size_renders(tabsize: usize) { - for width in [0, 1, 2, 3, 5, 10, 40, 130, 1000, 65535] { - assert_renders(b"a\tb\n", b"a\tc\n", width, tabsize); + #[test] + fn extreme_tab_size_renders() { + for tabsize in [ + 0, + 1, + 2, + 7, + 8, + 1000, + usize::MAX / 2, + usize::MAX - 1, + usize::MAX, + ] { + for width in [0, 1, 2, 3, 5, 10, 40, 130, 1000, 65535] { + assert_renders(b"a\tb\n", b"a\tc\n", width, tabsize); + } } } @@ -619,13 +622,18 @@ mod tests { diff(b"a\rb\n", b"c\n", &mut output, ¶ms); } - #[test_case(b"aaa\tbbb\n", b"aaa\tccc\n" ; "tabs on both sides")] - #[test_case(b"\t\t\n", b"\n" ; "tabs against an empty line")] - #[test_case("\u{4f60}\u{597d}\t\u{1f600}\n".as_bytes(), b"a\n" ; "wide and multibyte")] - fn every_small_width_and_tab_size_renders(from: &[u8], to: &[u8]) { - for width in 0..96 { - for tabsize in 0..96 { - assert_renders(from, to, width, tabsize); + #[test] + fn every_small_width_and_tab_size_renders() { + let cases: [(&[u8], &[u8]); 3] = [ + (b"aaa\tbbb\n", b"aaa\tccc\n"), + (b"\t\t\n", b"\n"), + ("\u{4f60}\u{597d}\t\u{1f600}\n".as_bytes(), b"a\n"), + ]; + for (from, to) in cases { + for width in 0..96 { + for tabsize in 0..96 { + assert_renders(from, to, width, tabsize); + } } } } From 3e17e59467c106ae3f07a542f869124c9b5301a9 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Mon, 5 Oct 2026 22:11:44 -0300 Subject: [PATCH 10/17] style(fuzz): shorten the width comment in the side-by-side target Cut the comment from five lines to two. The rewrite also drops the misspelled words that cspell would flag. --- fuzz/fuzz_targets/fuzz_side.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/fuzz/fuzz_targets/fuzz_side.rs b/fuzz/fuzz_targets/fuzz_side.rs index 3fddb94..8493fc9 100644 --- a/fuzz/fuzz_targets/fuzz_side.rs +++ b/fuzz/fuzz_targets/fuzz_side.rs @@ -8,11 +8,8 @@ use diffutilslib::params::Params; use std::fs::{self, File}; use std::io::Write; -// We can't fuzz with width equals to usize, otherwise we -// would could have 2^64 - 1 of padding columns, which means -// exabytes nescessary for this. u32 also doesn't have a -// great perfomance here, with almost 537 MB being nescessary -// and 57 seconds of execution. +// The width is a u16 because the output is padded up to it. A usize +// width needs exabytes, and a u32 one took 537 MB and 57 seconds. fuzz_target!(|x: (Vec, Vec, u16, usize, bool)| { let (original, new, width, tabsize, expand) = x; From 4b0350b8753a8ebd8c2dfea99c3702cc6c481fd9 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Mon, 5 Oct 2026 22:13:58 -0300 Subject: [PATCH 11/17] style(side-diff): restore the comment on the unpadded right line fc18aa5 deleted the comment above the `if !is_right` block in `process_half_line` without changing the code it describes. Put it back verbatim, since it still explains why only the left half is padded. --- src/side_diff.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/side_diff.rs b/src/side_diff.rs index 4654797..f29c2df 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -253,6 +253,7 @@ fn process_half_line( } } + // gnu sdiff do not tabulate the hole empty right line, instead, just keep the line empty if !is_right { // we always sum + 1 or + GUTTER_WIDTH_MIN cause we want to expand // up to the third column of the gutter column if the gutter is gutter white space, From 25d888b79a68f49b647f7d6dc20f9f72471712f9 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Mon, 5 Oct 2026 22:15:05 -0300 Subject: [PATCH 12/17] style(side-diff): restore the left and right file notes on diff fc18aa5 deleted the comment under the `diff` signature that marked `from_file` as the left file and `to_file` as the right one. Bring the notes back as trailing comments on the two parameters, since the original caret line no longer lines up with a signature that puts one parameter per line. --- src/side_diff.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/side_diff.rs b/src/side_diff.rs index f29c2df..228271c 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -339,8 +339,8 @@ fn push_output( } pub fn diff( - from_file: &[u8], - to_file: &[u8], + from_file: &[u8], // The left file + to_file: &[u8], // The right file output: &mut T, params: &Params, ) -> Vec { From 1cc188aef260aa8cb24fe106400590ff3394edb7 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Mon, 5 Oct 2026 22:17:14 -0300 Subject: [PATCH 13/17] test(side-diff): drop the duplicated width 130 layout assertions `expanded_tabs_widen_the_half_line` repeated two assertions that other tests already make: the plain layout for width 130 and tab size 8 is `default_width_and_tab_size`, and the expanded one is in `expanded_tabs_lay_out_as_a_stop_on_every_column`. The width 40 pair stays. No other layout test covers it. --- src/side_diff.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/side_diff.rs b/src/side_diff.rs index 228271c..04abb78 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -469,9 +469,6 @@ mod tests { #[test] fn expanded_tabs_widen_the_half_line() { - assert_layout(130, 8, 61, 64, 62); - assert_layout_expanded(130, 8, 63, 67, 64); - assert_layout(40, 8, 16, 24, 19); assert_layout_expanded(40, 8, 18, 22, 19); } From b538e929633597e39db368c432aaccd640beef06 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Mon, 5 Oct 2026 22:21:39 -0300 Subject: [PATCH 14/17] test(side-diff): assert on the rendered output The render sweeps only proved that `diff` does not panic. They now also check that the output has no tab when `expand_tabs` is set, for every width and tab size they cover. Add `changed_line_output` and `unchanged_line_output`, which compare the whole output for three cases at tab size 8: - a changed line at width 130 - a changed line at width 40 with `expand_tabs` - an unchanged line at width 130 The expected lines match what GNU diff 3.10 prints for a deleted, an added and an unchanged line at the same widths. --- src/side_diff.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/side_diff.rs b/src/side_diff.rs index 04abb78..c30d62a 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -585,9 +585,60 @@ mod tests { let mut output = vec![]; diff(from, to, &mut output, ¶ms); + + if expand_tabs { + assert!( + !output.contains(&b'\t'), + "tab in expanded output, width {width}, tab size {tabsize}" + ); + } } } + #[track_caller] + fn assert_output(from: &[u8], to: &[u8], width: usize, expand_tabs: bool, expected: &str) { + let params = Params { + width, + tabsize: 8, + expand_tabs, + ..Default::default() + }; + let mut output = vec![]; + + diff(from, to, &mut output, ¶ms); + + assert_eq!(String::from_utf8_lossy(&output), expected); + } + + #[test] + fn changed_line_output() { + assert_output( + b"a\tb\n", + b"a\tc\n", + 130, + false, + "a\tb\t\t\t\t\t\t <\n\t\t\t\t\t\t\t >\ta\tc\n", + ); + assert_output( + b"a\tb\n", + b"a\tc\n", + 40, + true, + "a b <\n > a c\n", + ); + } + + #[test] + fn unchanged_line_output() { + assert_output( + b"same\n", + b"same\n", + 130, + false, + "same\t\t\t\t\t\t\t\tsame\n", + ); + } + #[test] fn extreme_tab_size_renders() { for tabsize in [ From aa7a802c97126df5dbb1681b8a46b2aeba22f9d0 Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Mon, 5 Oct 2026 22:24:33 -0300 Subject: [PATCH 15/17] test(side-diff): assert the output at the widest line and tab size `carriage_return_at_the_widest_line_and_tab_size` only checked that `diff` does not abort. It now also compares the rendered output. --- src/side_diff.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/side_diff.rs b/src/side_diff.rs index c30d62a..ef84ecb 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -669,6 +669,8 @@ mod tests { let mut output = vec![]; diff(b"a\rb\n", b"c\n", &mut output, ¶ms); + + assert_eq!(String::from_utf8_lossy(&output), " <\n >\t\n"); } #[test] From cafc698e612276731c01ec111bd26366e0372f5c Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Mon, 5 Oct 2026 22:34:38 -0300 Subject: [PATCH 16/17] test(side-diff): adapt two tests to the gutter marker fix from main The merge brought in the fix for #269, which pads the gutter up to `separator_pos` before the marker. Two tests no longer held. - `carriage_return_at_the_widest_line_and_tab_size` ran `diff` with the width and the tab size both at `usize::MAX`. That padding is now 2^63 spaces, so the test never finished. It becomes `padding_to_the_end_of_the_widest_line_and_tab_size` and calls `format_tabs_and_spaces(0, usize::MAX, ...)` directly, which is the walk that used to overflow, and expects a single tab. - `sdiff_gutter_marker_column` expected the right column at 24 for `--width=40 --expand-tabs`, the offset of the layout that ignored `--expand-tabs`. This branch lays it out at column 22, which is where GNU diff 3.10 puts it. --- src/side_diff.rs | 13 ++++--------- tests/by-util/test_diff.rs | 5 +---- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/side_diff.rs b/src/side_diff.rs index ef84ecb..074f7a1 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -659,18 +659,13 @@ mod tests { } #[test] - fn carriage_return_at_the_widest_line_and_tab_size() { - let params = Params { - width: usize::MAX, - tabsize: usize::MAX, - expand_tabs: false, - ..Default::default() - }; + fn padding_to_the_end_of_the_widest_line_and_tab_size() { + let config = Config::new(usize::MAX, usize::MAX, false); let mut output = vec![]; - diff(b"a\rb\n", b"c\n", &mut output, ¶ms); + format_tabs_and_spaces(0, usize::MAX, &config, &mut output).unwrap(); - assert_eq!(String::from_utf8_lossy(&output), " <\n >\t\n"); + assert_eq!(output, b"\t"); } #[test] diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index dd416b0..8676c6f 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -865,10 +865,7 @@ fn sdiff_gutter_marker_column() -> Result<(), Box> { .arg(file1.path()) .arg(file2.path()); let output = cmd.output().unwrap().stdout; - assert_eq!( - output, - b"aa <\n > bb\n" - ); + assert_eq!(output, b"aa <\n > bb\n"); // Default (expand_tabs: false): padding goes through the tab path. let mut cmd = cargo_bin_cmd!("diffutils"); From c5d7be4cbb896c981fb296488042fda6d5b5dc5b Mon Sep 17 00:00:00 2001 From: Sami Daniel Date: Tue, 6 Oct 2026 00:10:58 -0300 Subject: [PATCH 17/17] Trigger CI