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 From 6091df6b7a0674a7215c5c1d2d93a1b37e9121b5 Mon Sep 17 00:00:00 2001 From: Sean Dewar <6256228+seandewar@users.noreply.github.com> Date: Tue, 19 Mar 2024 12:59:44 +0000 Subject: fix(api): nvim_create_buf assert fails if autocmds set &swapfile Problem: assertion failure in nvim_create_buf if buflist_new autocommands open a swapfile when "scratch" is set. Solution: block autocommands when setting up the buffer; fire them later instead. Note that, unlike buflist_new, I don't check if autocommands aborted script processing; the buffer is already created and configured at that point, so might as well return the handle anyway. Rather than repeat try_{start,end} and {un}block_autocmds for each relevant operation, just do it at the start and near the end. This means that, if TermResponse fires from unblock_autocmds for whatever reason, it can see the buffer in an already configured state if we didn't bail due to an error (plus it's probably a bit cleaner this way). --- src/nvim/api/vim.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 7a9094557d..43bf4eaf31 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -958,24 +958,22 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err) FUNC_API_SINCE(6) { try_start(); + // Block autocommands for now so they don't mess with the buffer before we + // finish configuring it. + block_autocmds(); + buf_T *buf = buflist_new(NULL, NULL, 0, BLN_NOOPT | BLN_NEW | (listed ? BLN_LISTED : 0)); - try_end(err); if (buf == NULL) { + unblock_autocmds(); goto fail; } // 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. - if (buf->b_ml.ml_mfp == NULL) { - try_start(); - block_autocmds(); - int status = ml_open(buf); + if (ml_open(buf) == FAIL) { unblock_autocmds(); - try_end(err); - if (status == FAIL) { - goto fail; - } + goto fail; } // Set last_changedtick to avoid triggering a TextChanged autocommand right @@ -985,7 +983,7 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err) buf->b_last_changedtick_pum = buf_get_changedtick(buf); // Only strictly needed for scratch, but could just as well be consistent - // and do this now. buffer is created NOW, not when it latter first happen + // and do this now. Buffer is created NOW, not when it later first happens // to reach a window or aucmd_prepbuf() .. buf_copy_options(buf, BCO_ENTER | BCO_NOHELP); @@ -996,10 +994,26 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err) buf->b_p_swf = false; buf->b_p_ml = false; } + + unblock_autocmds(); + + bufref_T bufref; + set_bufref(&bufref, buf); + if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, false, buf) + && !bufref_valid(&bufref)) { + goto fail; + } + if (listed + && apply_autocmds(EVENT_BUFADD, NULL, NULL, false, buf) + && !bufref_valid(&bufref)) { + goto fail; + } + + try_end(err); return buf->b_fnum; fail: - if (!ERROR_SET(err)) { + if (!try_end(err)) { api_set_error(err, kErrorTypeException, "Failed to create buffer"); } return 0; -- cgit