Skip to content

Commit c845f19

Browse files
committed
fix(terminal): patch various autocommand-related holes
Problem: autocommands can cause various problems in terminal mode, which can lead to crashes, for example. Solution: fix found issues. Move some checks to terminal_check and guard against autocommands messing with things. Trigger TermEnter/Leave after terminal mode has changed/restored most state. Wipeout the correct buffer if TermLeave switches buffers and fix a UAF if it or WinScrolled/Resized frees the terminal prematurely. These changes also allow us to remove the buffer restrictions on TextChangedT; they were inadequate in stopping some issues, and WinScrolled/Resized was lacking them anyway.
1 parent b7124ae commit c845f19

File tree

4 files changed

+193
-67
lines changed

4 files changed

+193
-67
lines changed

src/nvim/terminal.c

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,14 @@ void terminal_close(Terminal **termpp, int status)
601601
// If this was called by close_buffer() (status is -1), or if exiting, we
602602
// must inform the buffer the terminal no longer exists so that
603603
// close_buffer() won't call this again.
604-
// If inside Terminal mode K_EVENT handling, setting buf_handle to 0 also
604+
// If inside Terminal mode event handling, setting buf_handle to 0 also
605605
// informs terminal_enter() to call the close callback before returning.
606606
term->buf_handle = 0;
607607
if (buf) {
608608
buf->terminal = NULL;
609609
}
610610
if (!term->refcount) {
611-
// Not inside Terminal mode K_EVENT handling.
611+
// Not inside Terminal mode event handling.
612612
// We should not wait for the user to press a key.
613613
term->destroy = true;
614614
term->opts.close_cb(term->opts.data);
@@ -772,12 +772,13 @@ bool terminal_enter(void)
772772
adjust_topline(s->term, buf, 0); // scroll to end
773773
showmode();
774774
ui_cursor_shape();
775-
apply_autocmds(EVENT_TERMENTER, NULL, NULL, false, curbuf);
776-
may_trigger_modechanged();
777775

778776
// Tell the terminal it has focus
779777
terminal_focus(s->term, true);
780778

779+
apply_autocmds(EVENT_TERMENTER, NULL, NULL, false, curbuf);
780+
may_trigger_modechanged();
781+
781782
s->state.execute = terminal_execute;
782783
s->state.check = terminal_check;
783784
state_enter(&s->state);
@@ -792,8 +793,6 @@ bool terminal_enter(void)
792793
ui_busy_stop();
793794
}
794795

795-
apply_autocmds(EVENT_TERMLEAVE, NULL, NULL, false, curbuf);
796-
797796
// Restore the terminal cursor to what is set in 'guicursor'
798797
(void)parse_shape_opt(SHAPE_CURSOR);
799798

@@ -811,12 +810,19 @@ bool terminal_enter(void)
811810
unshowmode(true);
812811
}
813812
ui_cursor_shape();
813+
814+
// If we're to close the terminal, don't let TermLeave autocommands free it first!
815+
if (s->close) {
816+
s->term->refcount++;
817+
}
818+
apply_autocmds(EVENT_TERMLEAVE, NULL, NULL, false, curbuf);
814819
if (s->close) {
815-
bool wipe = s->term->buf_handle != 0;
820+
s->term->refcount--;
821+
const handle_T buf_handle = s->term->buf_handle; // Callback may free s->term.
816822
s->term->destroy = true;
817823
s->term->opts.close_cb(s->term->opts.data);
818-
if (wipe) {
819-
do_cmdline_cmd("bwipeout!");
824+
if (buf_handle != 0) {
825+
do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf_handle, true);
820826
}
821827
}
822828

@@ -840,24 +846,68 @@ static void terminal_check_cursor(void)
840846
coladvance(curwin, MAX(0, term->cursor.col + off));
841847
}
842848

843-
// Function executed before each iteration of terminal mode.
844-
// Return:
845-
// 1 if the iteration should continue normally
846-
// 0 if the main loop must exit
849+
static bool terminal_check_focus(TerminalState *const s)
850+
FUNC_ATTR_NONNULL_ALL
851+
{
852+
if (curbuf->terminal == NULL) {
853+
return false;
854+
}
855+
856+
if (s->save_curwin_handle != curwin->handle) {
857+
// Terminal window changed, update window options.
858+
unset_terminal_winopts(s);
859+
set_terminal_winopts(s);
860+
}
861+
if (s->term != curbuf->terminal) {
862+
// Active terminal buffer changed, flush terminal's cursor state to the UI.
863+
terminal_focus(s->term, false);
864+
865+
s->term = curbuf->terminal;
866+
s->term->pending.cursor = true;
867+
invalidate_terminal(s->term, -1, -1);
868+
terminal_focus(s->term, true);
869+
}
870+
return true;
871+
}
872+
873+
/// Function executed before each iteration of terminal mode.
874+
///
875+
/// @return:
876+
/// 1 if the iteration should continue normally
877+
/// 0 if the main loop must exit
847878
static int terminal_check(VimState *state)
848879
{
849880
TerminalState *const s = (TerminalState *)state;
850881

851-
if (stop_insert_mode) {
882+
if (stop_insert_mode || !terminal_check_focus(s)) {
852883
return 0;
853884
}
854885

855-
assert(s->term == curbuf->terminal);
886+
// Validate topline and cursor position for autocommands. Especially important for WinScrolled.
887+
terminal_check_cursor();
888+
validate_cursor(curwin);
889+
890+
// Don't let autocommands free the terminal from under our fingers.
891+
s->term->refcount++;
892+
if (must_redraw) {
893+
// TODO(seandewar): above changes will maybe change the behaviour of this more; untrollify this
894+
apply_autocmds(EVENT_TEXTCHANGEDT, NULL, NULL, false, curbuf);
895+
}
896+
may_trigger_win_scrolled_resized();
897+
s->term->refcount--;
898+
if (s->term->buf_handle == 0) {
899+
s->close = true;
900+
return 0;
901+
}
902+
903+
// Autocommands above may have changed focus, scrolled, or moved the cursor.
904+
if (!terminal_check_focus(s)) {
905+
return 0;
906+
}
856907
terminal_check_cursor();
857908
validate_cursor(curwin);
858-
const bool text_changed = must_redraw != 0;
859-
show_cursor_info_later(false);
860909

910+
show_cursor_info_later(false);
861911
if (must_redraw) {
862912
update_screen();
863913
} else {
@@ -866,21 +916,6 @@ static int terminal_check(VimState *state)
866916
showmode(); // clear cmdline and show mode
867917
}
868918
}
869-
if (text_changed) {
870-
// Make sure an invoked autocmd doesn't delete the buffer (and the
871-
// terminal) under our fingers.
872-
curbuf->b_locked++;
873-
874-
// save and restore curwin and curbuf, in case the autocmd changes them
875-
aco_save_T aco;
876-
aucmd_prepbuf(&aco, curbuf);
877-
apply_autocmds(EVENT_TEXTCHANGEDT, NULL, NULL, false, curbuf);
878-
aucmd_restbuf(&aco);
879-
880-
curbuf->b_locked--;
881-
}
882-
883-
may_trigger_win_scrolled_resized();
884919

885920
setcursor();
886921
refresh_cursor(s->term, &s->cursor_visible);
@@ -981,23 +1016,6 @@ static int terminal_execute(VimState *state, int key)
9811016
terminal_send_key(s->term, key);
9821017
}
9831018

984-
if (curbuf->terminal == NULL) {
985-
return 0;
986-
}
987-
if (s->save_curwin_handle != curwin->handle) {
988-
// Terminal window changed, update window options.
989-
unset_terminal_winopts(s);
990-
set_terminal_winopts(s);
991-
}
992-
if (s->term != curbuf->terminal) {
993-
// Active terminal buffer changed, flush terminal's cursor state to the UI
994-
terminal_focus(s->term, false);
995-
996-
s->term = curbuf->terminal;
997-
s->term->pending.cursor = true;
998-
invalidate_terminal(s->term, -1, -1);
999-
terminal_focus(s->term, true);
1000-
}
10011019
return 1;
10021020
}
10031021

test/functional/autocmd/termxx_spec.lua

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local uv = vim.uv
55

66
local clear, command, testprg = n.clear, n.command, n.testprg
77
local eval, eq, neq, retry = n.eval, t.eq, t.neq, t.retry
8+
local exec_lua = n.exec_lua
89
local matches = t.matches
910
local ok = t.ok
1011
local feed = n.feed
@@ -197,30 +198,63 @@ it('autocmd TermEnter, TermLeave', function()
197198
}, eval('g:evs'))
198199
end)
199200

200-
describe('autocmd TextChangedT', function()
201-
local screen
202-
before_each(function()
203-
clear()
204-
screen = tt.setup_screen()
205-
end)
201+
describe('autocmd TextChangedT,WinResized', function()
202+
before_each(clear)
206203

207-
it('works', function()
204+
it('TextChangedT works', function()
208205
command('autocmd TextChangedT * ++once let g:called = 1')
206+
tt.setup_screen()
209207
tt.feed_data('a')
210208
retry(nil, nil, function()
211209
eq(1, api.nvim_get_var('called'))
212210
end)
213211
end)
214212

215-
it('cannot delete terminal buffer', function()
216-
command('autocmd TextChangedT * bwipe!')
217-
tt.feed_data('a')
218-
screen:expect({ any = 'E937: ' })
219-
feed('<CR>')
220-
command('autocmd! TextChangedT')
221-
matches(
222-
'^E937: Attempt to delete a buffer that is in use: term://',
223-
api.nvim_get_vvar('errmsg')
224-
)
213+
it('no crash when deleting terminal buffer', function()
214+
-- Using nvim_open_term over :terminal as the former can free the terminal immediately on
215+
-- close, causing the crash.
216+
217+
-- WinResized
218+
local buf1, term1 = exec_lua(function()
219+
vim.cmd.new()
220+
local buf = vim.api.nvim_get_current_buf()
221+
local term = vim.api.nvim_open_term(0, {
222+
on_input = function()
223+
vim.cmd.wincmd '_'
224+
end,
225+
})
226+
vim.api.nvim_create_autocmd('WinResized', {
227+
once = true,
228+
command = 'bwipeout!',
229+
})
230+
return buf, term
231+
end)
232+
feed('ii')
233+
eq(false, api.nvim_buf_is_valid(buf1))
234+
eq('n', eval('mode()'))
235+
eq({}, api.nvim_get_chan_info(term1)) -- Channel should've been cleaned up.
236+
237+
-- TextChangedT
238+
local buf2, term2 = exec_lua(function()
239+
vim.cmd.new()
240+
local buf = vim.api.nvim_get_current_buf()
241+
local term = vim.api.nvim_open_term(0, {
242+
on_input = function(_, chan)
243+
vim.api.nvim_chan_send(chan, 'sup')
244+
end,
245+
})
246+
vim.api.nvim_create_autocmd('TextChangedT', {
247+
once = true,
248+
command = 'bwipeout!',
249+
})
250+
return buf, term
251+
end)
252+
feed('ii')
253+
-- refresh_terminal is deferred, so TextChangedT may not trigger immediately.
254+
retry(nil, nil, function()
255+
eq(false, api.nvim_buf_is_valid(buf2))
256+
end)
257+
eq('n', eval('mode()'))
258+
eq({}, api.nvim_get_chan_info(term2)) -- Channel should've been cleaned up.
225259
end)
226260
end)

test/functional/terminal/buffer_spec.lua

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,15 @@ describe(':terminal buffer', function()
345345
local chan = vim.api.nvim_open_term(0, {
346346
on_input = function(_, term, buf, data)
347347
if data == '\27[I' then
348+
vim.b[buf].term_focused = true
348349
vim.api.nvim_chan_send(term, 'focused\n')
349350
elseif data == '\27[O' then
351+
vim.b[buf].term_focused = false
350352
vim.api.nvim_chan_send(term, 'unfocused\n')
351353
end
352354
end,
353355
})
356+
vim.b.term_focused = false
354357
vim.api.nvim_chan_send(chan, '\27[?1004h') -- Enable focus reporting
355358
end
356359

@@ -367,6 +370,21 @@ describe(':terminal buffer', function()
367370
|
368371
]])
369372

373+
-- TermEnter/Leave happens *after* entering/leaving terminal mode, so focus should've changed
374+
-- already by the time these events run.
375+
exec_lua(function()
376+
_G.last_event = nil
377+
vim.api.nvim_create_autocmd({ 'TermEnter', 'TermLeave' }, {
378+
callback = function(args)
379+
_G.last_event = args.event
380+
.. ' '
381+
.. vim.fs.basename(args.file)
382+
.. ' '
383+
.. tostring(vim.b[args.buf].term_focused)
384+
end,
385+
})
386+
end)
387+
370388
feed('i')
371389
screen:expect([[
372390
focused │focused │ |
@@ -375,6 +393,8 @@ describe(':terminal buffer', function()
375393
{120:foo [-] }{119:foo [-] bar [-] }|
376394
{5:-- TERMINAL --} |
377395
]])
396+
eq('TermEnter foo true', exec_lua('return _G.last_event'))
397+
378398
-- Next window has the same terminal; no new notifications.
379399
command('wincmd w')
380400
screen:expect([[
@@ -403,6 +423,7 @@ describe(':terminal buffer', function()
403423
{119:foo [-] foo [-] }{120:bar [-] }|
404424
|
405425
]])
426+
eq('TermLeave bar false', exec_lua('return _G.last_event'))
406427
end)
407428
end)
408429

@@ -668,6 +689,58 @@ describe(':terminal buffer', function()
668689
unchanged = true,
669690
})
670691
end)
692+
693+
it('does not wipeout unrelated buffer after channel closes', function()
694+
local screen = Screen.new(50, 7)
695+
screen:set_default_attr_ids({
696+
[1] = { foreground = Screen.colors.Blue1, bold = true },
697+
[2] = { reverse = true },
698+
[31] = { background = Screen.colors.DarkGreen, foreground = Screen.colors.White, bold = true },
699+
})
700+
701+
local old_buf = api.nvim_get_current_buf()
702+
command('new')
703+
fn.chanclose(api.nvim_open_term(0, {}))
704+
local term_buf = api.nvim_get_current_buf()
705+
screen:expect([[
706+
^ |
707+
[Terminal closed] |
708+
{31:[Scratch] [-] }|
709+
|
710+
{1:~ }|
711+
{2:[No Name] }|
712+
|
713+
]])
714+
715+
-- Autocommand should not result in the wrong buffer being wiped out.
716+
command('autocmd TermLeave * ++once wincmd p')
717+
feed('ii')
718+
screen:expect([[
719+
^ |
720+
{1:~ }|*5
721+
|
722+
]])
723+
eq(old_buf, api.nvim_get_current_buf())
724+
eq(false, api.nvim_buf_is_valid(term_buf))
725+
726+
term_buf = api.nvim_get_current_buf()
727+
fn.chanclose(api.nvim_open_term(term_buf, {}))
728+
screen:expect([[
729+
^ |
730+
[Terminal closed] |
731+
|*5
732+
]])
733+
734+
-- Autocommand should not result in a heap UAF if it frees the terminal prematurely.
735+
command('autocmd TermLeave * ++once bwipeout!')
736+
feed('ii')
737+
screen:expect([[
738+
^ |
739+
{1:~ }|*5
740+
|
741+
]])
742+
eq(false, api.nvim_buf_is_valid(term_buf))
743+
end)
671744
end)
672745

673746
describe('on_lines does not emit out-of-bounds line indexes when', function()

0 commit comments

Comments
 (0)