From f3feae0bbfd415ad125f84199a7e2302540c2660 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 17 Feb 2026 21:00:13 +0800 Subject: [PATCH] fix(terminal): crash after deleting buffer lines (#37921) Problem: Terminal crashes after deleting buffer lines. Solution: Don't insert lines above lines 0. --- src/nvim/terminal.c | 2 ++ test/functional/terminal/scrollback_spec.lua | 25 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 3fe850e3df..4cc9dd7848 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -2470,6 +2470,8 @@ static void refresh_scrollback(Terminal *term, buf_T *buf) deleted--; } + // Clamp old_height in case buffer lines have been deleted by the user. + old_height = MIN(old_height, buf->b_ml.ml_line_count); while (term->sb_pending > 0) { // This means that either the window height has decreased or the screen // became full and libvterm had to push all rows up. Convert the first diff --git a/test/functional/terminal/scrollback_spec.lua b/test/functional/terminal/scrollback_spec.lua index 31d2a3ee30..e486d9dc48 100644 --- a/test/functional/terminal/scrollback_spec.lua +++ b/test/functional/terminal/scrollback_spec.lua @@ -1101,6 +1101,31 @@ describe('pending scrollback line handling', function() ]] assert_alive() end) + + it('does not crash after deleting buffer lines', function() + local buf = api.nvim_get_current_buf() + local chan = api.nvim_open_term(buf, {}) + api.nvim_chan_send(chan, ('a\n'):rep(11) .. 'a') + screen:expect([[ + ^a | + a |*5 + | + ]]) + api.nvim_set_option_value('modifiable', true, { buf = buf }) + api.nvim_buf_set_lines(buf, 0, -1, true, {}) + screen:expect([[ + ^ | + {1:~ }|*5 + | + ]]) + api.nvim_chan_send(chan, ('\nb'):rep(11) .. '\n') + screen:expect([[ + b |*5 + ^ | + | + ]]) + assert_alive() + end) end) describe('scrollback is correct', function()