mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge #2846 'out-of-bounds slicing'
This commit is contained in:
commit
366aa8c196
@ -108,7 +108,7 @@ ArrayOf(String) buffer_get_line_slice(Buffer buffer,
|
|||||||
Array rv = ARRAY_DICT_INIT;
|
Array rv = ARRAY_DICT_INIT;
|
||||||
buf_T *buf = find_buffer_by_handle(buffer, err);
|
buf_T *buf = find_buffer_by_handle(buffer, err);
|
||||||
|
|
||||||
if (!buf) {
|
if (!buf || !inbounds(buf, start)) {
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,6 +179,11 @@ void buffer_set_line_slice(Buffer buffer,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!inbounds(buf, start)) {
|
||||||
|
api_set_error(err, Validation, _("Index out of bounds"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
start = normalize_index(buf, start) + (include_start ? 0 : 1);
|
start = normalize_index(buf, start) + (include_start ? 0 : 1);
|
||||||
include_end = include_end || (end >= buf->b_ml.ml_line_count);
|
include_end = include_end || (end >= buf->b_ml.ml_line_count);
|
||||||
end = normalize_index(buf, end) + (include_end ? 1 : 0);
|
end = normalize_index(buf, end) + (include_end ? 1 : 0);
|
||||||
@ -550,3 +555,10 @@ static int64_t normalize_index(buf_T *buf, int64_t index)
|
|||||||
index = index > buf->b_ml.ml_line_count ? buf->b_ml.ml_line_count : index;
|
index = index > buf->b_ml.ml_line_count ? buf->b_ml.ml_line_count : index;
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns true if the 0-indexed `index` is within the 1-indexed buffer bounds.
|
||||||
|
static bool inbounds(buf_T *buf, int64_t index)
|
||||||
|
{
|
||||||
|
linenr_T nlines = buf->b_ml.ml_line_count;
|
||||||
|
return index >= -nlines && index < nlines;
|
||||||
|
}
|
||||||
|
@ -35,6 +35,20 @@ describe('buffer_* functions', function()
|
|||||||
eq('', curbuf('get_line', 0))
|
eq('', curbuf('get_line', 0))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('get_line: out-of-bounds returns empty string', function()
|
||||||
|
curbuf('set_line', 0, 'line1.a')
|
||||||
|
eq('', curbuf('get_line', 1))
|
||||||
|
eq('', curbuf('get_line', -2))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('set_line, del_line: out-of-bounds is an error', function()
|
||||||
|
curbuf('set_line', 0, 'line1.a')
|
||||||
|
eq(false, pcall(curbuf, 'set_line', 1, 'line1.b'))
|
||||||
|
eq(false, pcall(curbuf, 'set_line', -2, 'line1.b'))
|
||||||
|
eq(false, pcall(curbuf, 'del_line', 2))
|
||||||
|
eq(false, pcall(curbuf, 'del_line', -3))
|
||||||
|
end)
|
||||||
|
|
||||||
it('can handle NULs', function()
|
it('can handle NULs', function()
|
||||||
curbuf('set_line', 0, 'ab\0cd')
|
curbuf('set_line', 0, 'ab\0cd')
|
||||||
eq('ab\0cd', curbuf('get_line', 0))
|
eq('ab\0cd', curbuf('get_line', 0))
|
||||||
@ -43,6 +57,27 @@ describe('buffer_* functions', function()
|
|||||||
|
|
||||||
|
|
||||||
describe('{get,set}_line_slice', function()
|
describe('{get,set}_line_slice', function()
|
||||||
|
it('get_line_slice: out-of-bounds returns empty array', function()
|
||||||
|
curbuf('set_line_slice', 0, 0, true, true, {'a', 'b', 'c'})
|
||||||
|
eq({'a', 'b', 'c'}, curbuf('get_line_slice', 0, 2, true, true)) --sanity
|
||||||
|
|
||||||
|
eq({}, curbuf('get_line_slice', 2, 3, false, true))
|
||||||
|
eq({}, curbuf('get_line_slice', 3, 9, true, true))
|
||||||
|
eq({}, curbuf('get_line_slice', 3, -1, true, true))
|
||||||
|
eq({}, curbuf('get_line_slice', -3, -4, false, true))
|
||||||
|
eq({}, curbuf('get_line_slice', -4, -5, true, true))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('set_line_slice: out-of-bounds is an error', function()
|
||||||
|
curbuf('set_line_slice', 0, 0, true, true, {'a', 'b', 'c'})
|
||||||
|
eq({'a', 'b', 'c'}, curbuf('get_line_slice', 0, 2, true, true)) --sanity
|
||||||
|
|
||||||
|
eq({'c'}, curbuf('get_line_slice', -1, 4, true, true))
|
||||||
|
eq({'a', 'b', 'c'}, curbuf('get_line_slice', 0, 5, true, true))
|
||||||
|
eq(false, pcall(curbuf, 'set_line_slice', 4, 5, true, true, {'d'}))
|
||||||
|
eq(false, pcall(curbuf, 'set_line_slice', -4, -5, true, true, {'d'}))
|
||||||
|
end)
|
||||||
|
|
||||||
it('works', function()
|
it('works', function()
|
||||||
eq({''}, curbuf('get_line_slice', 0, -1, true, true))
|
eq({''}, curbuf('get_line_slice', 0, -1, true, true))
|
||||||
-- Replace buffer
|
-- Replace buffer
|
||||||
|
Loading…
Reference in New Issue
Block a user