grid: when setting the new viewport, ensure it’s correctly bounded

Do this by using scrollback relative coordinates, and ensure the new
viewport is not larger than (grid_rows - screen_rows), as that would
mean the viewport crosses the scrollback wrap-around.
This commit is contained in:
Daniel Eklöf 2022-08-29 20:46:19 +02:00
parent d810e4fc8e
commit 649d56a4e5
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

28
grid.c
View file

@ -1016,23 +1016,6 @@ grid_resize_and_reflow(
new_grid[idx] = grid_row_alloc(new_cols, true);
}
grid->view = view_follows ? grid->offset : viewport.row;
/* If enlarging the window, the old viewport may be too far down,
* with unallocated rows. Make sure this cannot happen */
while (true) {
int idx = (grid->view + new_screen_rows - 1) & (new_rows - 1);
if (new_grid[idx] != NULL)
break;
grid->view--;
if (grid->view < 0)
grid->view += new_rows;
}
for (size_t r = 0; r < new_screen_rows; r++) {
int UNUSED idx = (grid->view + r) & (new_rows - 1);
xassert(new_grid[idx] != NULL);
}
/* Free old grid (rows already free:d) */
free(grid->rows);
@ -1040,6 +1023,17 @@ grid_resize_and_reflow(
grid->num_rows = new_rows;
grid->num_cols = new_cols;
/*
* Set new viewport, making sure its not too far down.
*
* This is done by using scrollback-start relative cooardinates,
* and bounding the new viewport to (grid_rows - screen_rows).
*/
int sb_view = grid_row_abs_to_sb(
grid, new_screen_rows, view_follows ? grid->offset : viewport.row);
grid->view = grid_row_sb_to_abs(
grid, new_screen_rows, min(sb_view, new_rows - new_screen_rows));
/* Convert absolute coordinates to screen relative */
cursor.row -= grid->offset;
while (cursor.row < 0)