From d5c23d72a5e4d2abb0903e58c4953fa0303d4ad6 Mon Sep 17 00:00:00 2001 From: Sean Dewar <6256228+seandewar@users.noreply.github.com> Date: Tue, 19 Mar 2024 10:55:33 +0000 Subject: fix(api): nvim_create_buf leaks memory if buffer is loaded early Problem: memory leak in nvim_create_buf if buflist_new autocommands load the new buffer early. Solution: do not open a memfile in that case. --- src/nvim/api/vim.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 24ad7d5fbc..7a9094557d 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -967,13 +967,15 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err) // Open the memline for the buffer. This will avoid spurious autocmds when // a later nvim_buf_set_lines call would have needed to "open" the buffer. - try_start(); - block_autocmds(); - int status = ml_open(buf); - unblock_autocmds(); - try_end(err); - if (status == FAIL) { - goto fail; + if (buf->b_ml.ml_mfp == NULL) { + try_start(); + block_autocmds(); + int status = ml_open(buf); + unblock_autocmds(); + try_end(err); + if (status == FAIL) { + goto fail; + } } // Set last_changedtick to avoid triggering a TextChanged autocommand right -- cgit