fix(api): clamp range lines in nvim__redraw() (#31710)

Problem:
`nvim__redraw()` doesn't clamp the lines in the `range` parameter before truncating to int. The resulting range may be empty when the original range contained buffer lines and vice versa.

E.g. for a buffer with 4 lines, these are the redrawn lines:

```lua
{ 2, 2 ^ 31 } -> none (should be { 2, 3 })
{ 2, 2 ^ 32 } -> none (should be { 2, 3 })
{ 2 ^ 32 - 1, 2 } -> { 0, 1 } (should be none)
```

Solution:
Clamp `range` values before truncating to int.
This commit is contained in:
Artem
2024-12-25 14:12:40 -06:00
committed by GitHub
parent 7567f7d322
commit 487c48ec86
2 changed files with 62 additions and 7 deletions

View File

@@ -2374,13 +2374,23 @@ void nvim__redraw(Dict(redraw) *opts, Error *err)
"%s", "Invalid 'range': Expected 2-tuple of Integers", {
return;
});
linenr_T first = (linenr_T)kv_A(opts->range, 0).data.integer + 1;
linenr_T last = (linenr_T)kv_A(opts->range, 1).data.integer;
int64_t begin_raw = kv_A(opts->range, 0).data.integer;
int64_t end_raw = kv_A(opts->range, 1).data.integer;
buf_T *rbuf = win ? win->w_buffer : (buf ? buf : curbuf);
if (last == -1) {
last = rbuf->b_ml.ml_line_count;
linenr_T line_count = rbuf->b_ml.ml_line_count;
int begin = (int)MIN(begin_raw, line_count);
int end;
if (end_raw == -1) {
end = line_count;
} else {
end = (int)MIN(MAX(begin, end_raw), line_count);
}
if (begin < end) {
redraw_buf_range_later(rbuf, 1 + begin, end);
}
redraw_buf_range_later(rbuf, first, last);
}
// Redraw later types require update_screen() so call implicitly unless set to false.