diff: `-y --tabsize=<huge>` overflows the column arithmetic (overflow-checks only)

Open Beginner friendly
#264 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
70/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
rust
Domain
cli

Research direction

Start in src/side_diff.rs at SideDiffConfig::new and inspect the arithmetic around line 85, including how --tabsize reaches tab_size. Reproduce the provided command with overflow checks enabled and verify that extreme and zero tab sizes no longer panic or produce invalid layout widths.

Written by the indexing model from the issue text.

Description

SideDiffConfig::new (src/side_diff.rs) computes the side-by-side column layout with signed arithmetic on the --tabsize value:

let w = full_width as isize;
let t = tab_size as isize;
let t_plus_g = t + GUTTER_WIDTH_MIN as isize;      // <-- side_diff.rs:85: overflows
let unaligned_off = (w >> 1) + (t_plus_g >> 1) + (w & t_plus_g & 1);
let off = unaligned_off - unaligned_off % t;       // (also % by t, and t can be 0)

--tabsize is parsed into tab_size: usize with no upper bound. A value near isize::MAX makes t + GUTTER_WIDTH_MIN (GUTTER_WIDTH_MIN == 3) overflow isize. Under -C overflow-checks=on this aborts (exit 134); in the default release build it wraps to a bogus value and the layout math silently produces garbage widths.

$ printf 'a\n' > f1; printf 'b\n' > f2
$ diff -y --tabsize=9223372036854775805 f1 f2      # overflow-checks build
thread 'main' panicked at src/side_diff.rs:85:24:
attempt to add with overflow
$ echo $?
134
Dominant language
Rust
Stars
276
Forks
39
Avg merge
3h 27m
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from uutils/diffutils

All issues in uutils/diffutils

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.