api: nvim_buf_set_lines(): Avoid invalid ml_get. #6019

When the buffer that nvim_buf_set_lines() is changing is not in any vim
window, fix_cursor() leads to calling ml_get_buf() with an invalid line
number. The condition that fix_cursor() was called on was (buf ==
curbuf), but this is always true because of the call to
switch_to_win_for_buf() earlier in the function.

Instead this should be predicated on (save_curbuf.br_buf == NULL)
This commit is contained in:
Matthew Malcomson 2017-01-27 12:09:33 +00:00 committed by Justin M. Keyes
parent 1215084676
commit 6fe677c108
2 changed files with 20 additions and 1 deletions

View File

@ -395,7 +395,7 @@ void nvim_buf_set_lines(uint64_t channel_id,
changed_lines((linenr_T)start, 0, (linenr_T)end, extra);
if (buf == curbuf) {
if (save_curbuf.br_buf == NULL) {
fix_cursor((linenr_T)start, (linenr_T)end, extra);
}

View File

@ -4,6 +4,9 @@ local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq
local curbufmeths, ok = helpers.curbufmeths, helpers.ok
local funcs = helpers.funcs
local request = helpers.request
local exc_exec = helpers.exc_exec
local execute = helpers.execute
local insert = helpers.insert
local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall
local command = helpers.command
@ -242,6 +245,22 @@ describe('api/buf', function()
eq({'e', 'a', 'b', 'c', 'd'}, get_lines(0, -1, true))
end)
it("set_line on alternate buffer does not access invalid line (E315)", function()
execute('set hidden')
insert('Initial file')
command('enew')
insert([[
More
Lines
Than
In
The
Other
Buffer]])
execute('$')
local retval = exc_exec("call nvim_buf_set_lines(1, 0, 1, v:false, ['test'])")
eq(0, retval)
end)
end)
describe('{get,set,del}_var', function()