Skip to content

Commit a9ffdca

Browse files
seandewarzeertzjq
authored andcommitted
fix(api): open_win leak from naughty autocommands
Problem: error set by win_set_buf may leak if autocommands immediately close the new window. Solution: free the error set by win_set_buf. (prefer nvim_open_win's error as it's more important and will cause 0 to be returned)
1 parent 91ebbc6 commit a9ffdca

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/nvim/api/win_config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(win_config) *config, Err
343343
}
344344
}
345345
if (!tp) {
346+
api_clear_error(err); // may have been set by win_set_buf
346347
api_set_error(err, kErrorTypeException, "Window was closed immediately");
347348
goto cleanup;
348349
}

test/functional/api/window_spec.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,31 @@ describe('API/win', function()
20682068
api.nvim_open_win(0, true, { split = 'below' })
20692069
eq(11, api.nvim_win_get_height(0))
20702070
end)
2071+
2072+
it('no leak when win_set_buf fails and window is closed immediately', function()
2073+
-- Following used to leak.
2074+
command('autocmd BufEnter * ++once quit! | throw 1337')
2075+
eq(
2076+
'Window was closed immediately',
2077+
pcall_err(
2078+
api.nvim_open_win,
2079+
api.nvim_create_buf(true, true),
2080+
true,
2081+
{ relative = 'editor', width = 5, height = 5, row = 1, col = 1 }
2082+
)
2083+
)
2084+
-- If the window wasn't closed, still set errors from win_set_buf.
2085+
command('autocmd BufEnter * ++once throw 1337')
2086+
eq(
2087+
'BufEnter Autocommands for "*": 1337',
2088+
pcall_err(
2089+
api.nvim_open_win,
2090+
api.nvim_create_buf(true, true),
2091+
true,
2092+
{ relative = 'editor', width = 5, height = 5, row = 1, col = 1 }
2093+
)
2094+
)
2095+
end)
20712096
end)
20722097

20732098
describe('set_config', function()

0 commit comments

Comments
 (0)