aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2015-07-11 22:33:57 -0400
committerJustin M. Keyes <justinkz@gmail.com>2015-07-11 22:33:57 -0400
commit366aa8c1963fadabc164e823b6ba758759a30178 (patch)
tree1f755096202e55c4b6557b6555b6b013fa7aebac /src
parent5fdaac45a60cb555579fd2f10fad7e52c67ae042 (diff)
parent7b56a8230fcb57ab63dbc2923dfa3c4bcec42edd (diff)
downloadrneovim-366aa8c1963fadabc164e823b6ba758759a30178.tar.gz
rneovim-366aa8c1963fadabc164e823b6ba758759a30178.tar.bz2
rneovim-366aa8c1963fadabc164e823b6ba758759a30178.zip
Merge #2846 'out-of-bounds slicing'
Diffstat (limited to 'src')
-rw-r--r--src/nvim/api/buffer.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index c9ada8dfc0..915c5f74d7 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;
}
@@ -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);
@@ -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;
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;
+}