From 90b4276d67b399141be9db01092b15566c61e5e6 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Tue, 16 Jun 2015 20:04:43 -0400 Subject: api: return empty array when slicing out of bounds. --- src/nvim/api/buffer.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/nvim/api/buffer.c') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index c9ada8dfc0..9c29d41009 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -108,7 +108,7 @@ ArrayOf(String) buffer_get_line_slice(Buffer buffer, Array rv = ARRAY_DICT_INIT; buf_T *buf = find_buffer_by_handle(buffer, err); - if (!buf) { + if (!buf || !inbounds(buf, start)) { return rv; } @@ -550,3 +550,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; 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; +} -- cgit From 7475c1c0f795912ea5a4613a23c4ef5a1d88c24d Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Tue, 16 Jun 2015 20:07:03 -0400 Subject: api: return error when starting a slice out of bounds. --- src/nvim/api/buffer.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/nvim/api/buffer.c') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 9c29d41009..915c5f74d7 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -179,6 +179,11 @@ void buffer_set_line_slice(Buffer buffer, return; } + if (!inbounds(buf, start)) { + api_set_error(err, Validation, _("Index out of bounds")); + return; + } + start = normalize_index(buf, start) + (include_start ? 0 : 1); include_end = include_end || (end >= buf->b_ml.ml_line_count); end = normalize_index(buf, end) + (include_end ? 1 : 0); -- cgit