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);