vim-patch:9.1.1387: memory leak when buflist_new() fails to reuse curbuf

Problem:  buflist_new() leaks ffname and fails to reuse curbuf when
          autocommands from buf_freeall change curbuf. Plus, a new
          buffer is not allocated in this case, despite what the comment
          above claims.
Solution: Remove the condition so ffname is not leaked and so a new
          buffer is allocated like before v8.2.4791. It should not be
          possible for undo_ftplugin or buf_freeall autocommands to
          delete the buffer as they set b_locked, but to stay consistent
          with other uses of buf_freeall, guard against that anyway
          (Sean Dewar).

Note that buf is set to NULL if it was deleted to guard against the (rare)
possibility of messing up the "buf != curbuf" condition below if a new buffer
happens to be allocated at the same address.

closes: vim/vim#17319

0077282c82

Co-authored-by: Sean Dewar <6256228+seandewar@users.noreply.github.com>
(cherry picked from commit 6b9665a507)
This commit is contained in:
Sean Dewar
2025-05-14 19:39:13 +01:00
committed by github-actions[bot]
parent f623fad9c4
commit d50f71d2f1
2 changed files with 31 additions and 3 deletions

View File

@@ -4342,4 +4342,29 @@ func Test_eventignorewin_non_current()
%bw!
endfunc
func Test_reuse_curbuf_leak()
new bar
let s:bar_buf = bufnr()
augroup testing
autocmd!
autocmd BufDelete * ++once let s:triggered = 1 | execute s:bar_buf 'buffer'
augroup END
enew
let empty_buf = bufnr()
" Old curbuf should be reused, firing BufDelete. As BufDelete changes curbuf,
" reusing the buffer would fail and leak the ffname.
edit foo
call assert_equal(1, s:triggered)
" Wasn't reused because the buffer changed, but buffer "foo" is still created.
call assert_equal(1, bufexists(empty_buf))
call assert_notequal(empty_buf, bufnr())
call assert_equal('foo', bufname())
call assert_equal('bar', bufname(s:bar_buf))
unlet! s:bar_buf s:triggered
call CleanUpTestAuGroup()
%bw!
endfunc
" vim: shiftwidth=2 sts=2 expandtab