Skip to content

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

Description

@leeewee

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions