diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 533fa95e99..03ab2a76ad 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -1593,6 +1593,10 @@ static void init_prompt(int cmdchar_todo) char *prompt = prompt_text(); 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); + 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); colnr_T text_len = ml_get_len(curbuf->b_prompt_start.mark.lnum); diff --git a/test/functional/legacy/prompt_buffer_spec.lua b/test/functional/legacy/prompt_buffer_spec.lua index 1acc6a2258..2b2f73f5dc 100644 --- a/test/functional/legacy/prompt_buffer_spec.lua +++ b/test/functional/legacy/prompt_buffer_spec.lua @@ -671,6 +671,13 @@ describe('prompt buffer', function() -- No crash from invalid col. eq(true, api.nvim_buf_set_mark(0, ':', fn('line', '.'), 999, {})) eq({ 12, 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, ':')) end) describe('prompt_getinput', function()