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

@@ -500,7 +500,7 @@ static bool can_unload_buffer(buf_T *buf)
/// close all other windows.
/// @param ignore_abort
/// If true, don't abort even when aborting() returns true.
/// @return true when we got to the end and b_nwindows was decremented.
/// @return true if b_nwindows was decremented directly by this call (e.g: not via autocmds).
bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool ignore_abort)
{
bool unload_buf = (action != 0);
@@ -625,7 +625,7 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i
// Return when a window is displaying the buffer or when it's not
// unloaded.
if (buf->b_nwindows > 0 || !unload_buf) {
return false;
return true;
}
if (buf->terminal) {
@@ -699,7 +699,7 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i
if (wipe_buf) {
// Do not wipe out the buffer if it is used in a window.
if (buf->b_nwindows > 0) {
return false;
return true;
}
FOR_ALL_TAB_WINDOWS(tp, wp) {
mark_forget_file(wp, buf->b_fnum);