Wilfred / difftastic

a structural diff that understands syntax 🟥🟩
https://difftastic.wilfred.me.uk/
MIT License
20.59k stars 333 forks source link

Crash inside eshell with memory allocation error #707

Closed sunlin7 closed 4 months ago

sunlin7 commented 5 months ago

Hi, I tried difft inside eshell and got crash, just a simple diff two files:

$ difft fileA fileB memory allocation of 9223372036854775803 bytes failed aborted

The eshell is built-in emacs-30.0.50, and difft is the Difftastic 0.57.0 (00e837a 2024-04-01, built with rustc 1.65.0).

Wilfred commented 5 months ago

Could you share the files that caused this issue?

sunlin7 commented 5 months ago

Hi @Wilfred I attach a.el and b.el which will lead a crash message in my local with eshell inside emacs. test.TGZ

$ emacs -nw -q # and start eshell /tmp $ difft a.el b.el b.el --- Emacs Lisp File permissions changed from 100664 to 100600. memory allocation of 9223372036854775804 bytes failed aborted

bernhard-herzog commented 4 months ago

With a debug build I get a more maningful error and backtrace:

3: difft::display::side_by_side::SourceDimensions::new
          at ./src/display/side_by_side.rs:185:31

That's

        let lhs_total_width = (terminal_width - SPACER.len()) / 2;

When running in eshell, terminal_width is 0 and SPACER.len() is positive, hence the overflow.

AFAICT, terminal_width is 0, because in detect_display_width in ./src/options.rs:864 there's

    if let Ok((columns, _rows)) = crossterm::terminal::size() {

When running in eshell, crossterm::terminal::size() returns Ok, but with colums and rows both 0.

So the cause is that crossterm cannot correctly determine the size of the terminal in eshell. The best fix is probably to fix crossterm, but as a workaround, difftastic could check for a 0 width here and fall back on e.g. looking at the COLUMNS environment variable which is set correctly in eshell or the default value of 80.

Wilfred commented 4 months ago

Reported upstream: https://github.com/crossterm-rs/crossterm/issues/891

sunlin7 commented 4 months ago

Thank you all for the great works!