Wilfred / difftastic

a structural diff that understands syntax 🟥🟩

Home Page:https://difftastic.wilfred.me.uk/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash inside eshell with memory allocation error

sunlin7 opened this issue · comments

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).

Could you share the files that caused this issue?

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

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.

Thank you all for the great works!