fix(prompt): prompt_setprompt with unloaded buffer, ': with lnum 0

Problem: prompt_setprompt memory leak/other issues when fixing prompt line for
unloaded buffer, or when ': line number is zero.

Solution: don't fix prompt line for unloaded buffer. Clamp ': lnum above zero.
This commit is contained in:
Sean Dewar
2026-02-16 22:09:14 +00:00
parent 13cf80deef
commit 16bf7652b7
3 changed files with 19 additions and 5 deletions

View File

@@ -1594,8 +1594,8 @@ static void init_prompt(int cmdchar_todo)
int prompt_len = (int)strlen(prompt);
// In case the mark is set to a nonexistent line.
curbuf->b_prompt_start.mark.lnum = MIN(curbuf->b_prompt_start.mark.lnum,
curbuf->b_ml.ml_line_count);
curbuf->b_prompt_start.mark.lnum = MAX(1, MIN(curbuf->b_prompt_start.mark.lnum,
curbuf->b_ml.ml_line_count));
curwin->w_cursor.lnum = MAX(curwin->w_cursor.lnum, curbuf->b_prompt_start.mark.lnum);
char *text = ml_get(curbuf->b_prompt_start.mark.lnum);

View File

@@ -772,9 +772,10 @@ void f_prompt_setprompt(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
// Update the prompt-text and prompt-marks if a plugin calls prompt_setprompt()
// even while user is editing their input.
if (bt_prompt(buf)) {
if (bt_prompt(buf) && buf->b_ml.ml_mfp != NULL) {
// In case the mark is set to a nonexistent line.
buf->b_prompt_start.mark.lnum = MIN(buf->b_prompt_start.mark.lnum, buf->b_ml.ml_line_count);
buf->b_prompt_start.mark.lnum = MAX(1, MIN(buf->b_prompt_start.mark.lnum,
buf->b_ml.ml_line_count));
linenr_T prompt_lno = buf->b_prompt_start.mark.lnum;
char *old_prompt = buf_prompt_text(buf);

View File

@@ -691,12 +691,19 @@ describe('prompt buffer', function()
eq(true, api.nvim_buf_set_mark(0, ':', fn('line', '.'), 999, {}))
eq({ 12, 6 }, api.nvim_buf_get_mark(0, ':'))
-- Clamps lnum to at least 1. Do in one event to repro the leak.
exec_lua(function()
vim.fn.setpos("':", { 0, 0, 0, 0 })
vim.fn.prompt_setprompt('', 'bar > ')
end)
eq({ 1, 6 }, api.nvim_buf_get_mark(0, ':'))
-- No ml_get error from invalid lnum.
command('set messagesopt+=wait:0 messagesopt-=hit-enter')
fn('setpos', "':", { 0, 999, 7, 0 })
eq('', api.nvim_get_vvar('errmsg'))
command('set messagesopt&')
eq({ 12, 6 }, api.nvim_buf_get_mark(0, ':'))
eq({ 13, 6 }, api.nvim_buf_get_mark(0, ':'))
end)
describe('prompt_getinput', function()
@@ -952,5 +959,11 @@ describe('prompt buffer', function()
{1:~ }|*8
{5:-- INSERT --} |
]])
-- No leak if prompt_setprompt called for unloaded prompt buffer.
local unloaded_buf = fn('bufadd', '')
api.nvim_set_option_value('buftype', 'prompt', { buf = unloaded_buf })
fn('prompt_setprompt', unloaded_buf, 'hello unloaded! > ')
eq('hello unloaded! > ', fn('prompt_getprompt', unloaded_buf))
end)
end)