fix(window): restore b_nwindows if win_close_othertab keeps window

Problem: can't accurately know if close_buffer directly (e.g: not via autocmds)
decremented b_nwindows. This can cause crashes if win_close_othertab decides to
keep the window after calling close_buffer (if it did not free the buffer), as
b_nwindows may remain out-of-sync.

Solution: change the return value of close_buffer to accurately depict whether
it decremented b_nwindows. Check it in win_close_othertab to avoid a crash.

Similar issues may exist in other places that call close_buffer, but I've not
addressed those here (not to mention only one other place even checks its return
value...)
This commit is contained in:
Sean Dewar
2025-07-16 20:44:18 +01:00
parent 1d2d6e31a7
commit 9813c00dd3
3 changed files with 36 additions and 9 deletions

View File

@@ -2209,6 +2209,23 @@ describe('API/win', function()
pcall_err(command, 'call nvim_win_close(g:win, 0)')
)
eq(true, eval 'nvim_tabpage_is_valid(g:tp)')
exec([[
tabnew
let g:tp = nvim_get_current_tabpage()
let g:win = win_getid()
let g:buf = bufnr()
tabprevious
let s:buf2 = nvim_create_buf(0, 0)
call setbufvar(s:buf2, '&modified', 1)
call setbufvar(s:buf2, '&bufhidden', 'wipe')
autocmd! WinClosed * ++once call nvim_open_win(s:buf2, 0, #{win: g:win, relative: 'win', width: 5, height: 5, row: 5, col: 5})
]])
matches(
'E5601: Cannot close window, only floating window would remain$',
pcall_err(command, 'call nvim_buf_delete(g:buf, #{force: 1})')
)
eq(true, eval 'nvim_tabpage_is_valid(g:tp)')
end)
end)