vim-patch:8.2.3476: renaming a buffer on startup may cause using freed memory

Problem:    Renaming a buffer on startup may cause using freed memory.
Solution:   Check if the buffer is used in a window. (closes vim/vim#8955)

d3710cf01e

Cherry-pick Test_echo_true_in_cmd() from Vim.

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq
2024-08-08 14:51:29 +08:00
parent f35d5afbf1
commit 92186be428
2 changed files with 41 additions and 1 deletions

View File

@@ -2959,7 +2959,17 @@ int setfname(buf_T *buf, char *ffname_arg, char *sfname_arg, bool message)
obuf = buflist_findname_file_id(ffname, &file_id, file_id_valid);
}
if (obuf != NULL && obuf != buf) {
if (obuf->b_ml.ml_mfp != NULL) { // it's loaded, fail
bool in_use = false;
// during startup a window may use a buffer that is not loaded yet
FOR_ALL_TAB_WINDOWS(tab, win) {
if (win->w_buffer == obuf) {
in_use = true;
}
}
// it's loaded or used in a window, fail
if (obuf->b_ml.ml_mfp != NULL || in_use) {
if (message) {
emsg(_("E95: Buffer with this name already exists"));
}