diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-06-23 21:38:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-23 21:38:00 +0800 |
commit | 3c85fd817ee57793503b29202ce07166f0a48480 (patch) | |
tree | 28ca2dbd2bb0b20d0d4f79a80ea11554b8727f29 /src/nvim/api/buffer.c | |
parent | 7718b758461265d8966468c104ce5454538471e2 (diff) | |
download | rneovim-3c85fd817ee57793503b29202ce07166f0a48480.tar.gz rneovim-3c85fd817ee57793503b29202ce07166f0a48480.tar.bz2 rneovim-3c85fd817ee57793503b29202ce07166f0a48480.zip |
fix(api): check for inclusive buffer line index out of bounds correctly (#19056)
Diffstat (limited to 'src/nvim/api/buffer.c')
-rw-r--r-- | src/nvim/api/buffer.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 0810d155f6..1504004c6c 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -554,13 +554,13 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In // check range is ordered and everything! // start_row, end_row within buffer len (except add text past the end?) start_row = normalize_index(buf, start_row, false, &oob); - if (oob || start_row == buf->b_ml.ml_line_count + 1) { + if (oob) { api_set_error(err, kErrorTypeValidation, "start_row out of bounds"); return; } end_row = normalize_index(buf, end_row, false, &oob); - if (oob || end_row == buf->b_ml.ml_line_count + 1) { + if (oob) { api_set_error(err, kErrorTypeValidation, "end_row out of bounds"); return; } @@ -1359,14 +1359,15 @@ static void fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) // Normalizes 0-based indexes to buffer line numbers static int64_t normalize_index(buf_T *buf, int64_t index, bool end_exclusive, bool *oob) { - int64_t line_count = buf->b_ml.ml_line_count; + assert(buf->b_ml.ml_line_count > 0); + int64_t max_index = buf->b_ml.ml_line_count + (int)end_exclusive - 1; // Fix if < 0 - index = index < 0 ? line_count + index + (int)end_exclusive : index; + index = index < 0 ? max_index + index + 1 : index; // Check for oob - if (index > line_count) { + if (index > max_index) { *oob = true; - index = line_count; + index = max_index; } else if (index < 0) { *oob = true; index = 0; |