Skip to content

Commit

Permalink
Merge pull request #18942 from neovim/backport-18933-to-release-0.7
Browse files Browse the repository at this point in the history
[Backport release-0.7] fix(buffer): disable buffer-updates before removing buffer from window
  • Loading branch information
zeertzjq committed Jun 12, 2022
2 parents 1dba6cf + 05f6883 commit 3cea4d6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/nvim/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,10 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i
return false;
}

// Disable buffer-updates for the current buffer.
// No need to check `unload_buf`: in that case the function returned above.
buf_updates_unload(buf, false);

if (win != NULL // Avoid bogus clang warning.
&& win_valid_any_tab(win)
&& win->w_buffer == buf) {
Expand All @@ -590,10 +594,6 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i
buf->b_nwindows--;
}

// Disable buffer-updates for the current buffer.
// No need to check `unload_buf`: in that case the function returned above.
buf_updates_unload(buf, false);

// Remove the buffer from the list.
if (wipe_buf) {
// Do not wipe out the buffer if it is used in a window.
Expand Down
2 changes: 1 addition & 1 deletion src/nvim/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -7207,7 +7207,7 @@ void win_findbuf(typval_T *argvars, list_T *list)
int bufnr = tv_get_number(&argvars[0]);

FOR_ALL_TAB_WINDOWS(tp, wp) {
if (!wp->w_closing && wp->w_buffer->b_fnum == bufnr) {
if (wp->w_buffer->b_fnum == bufnr) {
tv_list_append_number(list, wp->handle);
}
}
Expand Down
11 changes: 8 additions & 3 deletions test/functional/lua/buffer_updates_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,28 @@ describe('lua buffer event callbacks: on_lines', function()
eq(2, meths.win_get_cursor(0)[1])
end)

it('does not SEGFAULT when calling win_findbuf in on_detach', function()

exec_lua[[
it('does not SEGFAULT when accessing window buffer info in on_detach #14998', function()
local code = [[
local buf = vim.api.nvim_create_buf(false, false)
vim.cmd"split"
vim.api.nvim_win_set_buf(0, buf)
vim.api.nvim_buf_attach(buf, false, {
on_detach = function(_, buf)
vim.fn.tabpagebuflist()
vim.fn.win_findbuf(buf)
end
})
]]

exec_lua(code)
command("q!")
helpers.assert_alive()

exec_lua(code)
command("bd!")
helpers.assert_alive()
end)

it('#12718 lnume', function()
Expand Down

0 comments on commit 3cea4d6

Please sign in to comment.