From ad74c5ed7f62913d38543ff6843f38d14689b56d Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Mon, 29 Jun 2026 10:20:38 +0100 Subject: [PATCH] Add additional grid consistency checks for macOS. --- grid.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/grid.c b/grid.c index 44efa574d..28680cbdd 100644 --- a/grid.c +++ b/grid.c @@ -18,6 +18,9 @@ #include +#ifdef __APPLE__ +#include +#endif #include #include @@ -56,6 +59,28 @@ static const struct grid_cell_entry grid_cleared_entry = { { .data = { 0, 8, 8, ' ' } }, GRID_FLAG_CLEARED }; +#ifdef __APPLE__ +static void +grid_check_lines(struct grid *gd) +{ + u_int i, j; + + for (i = 0; i < gd->hsize + gd->sy; i++) { + for (j = i + 1; j < gd->hsize + gd->sy; j++) { + if (gd->linedata[i].celldata != NULL) + assert(gd->linedata[i].celldata != gd->linedata[j].celldata); + if (gd->linedata[i].extddata != NULL) + assert(gd->linedata[i].extddata != gd->linedata[j].extddata); + } + } +} +#else +static void +grid_check_lines(__unused struct grid *gd) +{ +} +#endif + /* Store cell in entry. */ static void grid_store_cell(struct grid_cell_entry *gce, const struct grid_cell *gc, @@ -287,9 +312,17 @@ grid_set_tab(struct grid_cell *gc, u_int width) static void grid_free_line(struct grid *gd, u_int py) { - free(gd->linedata[py].celldata); - free(gd->linedata[py].extddata); - memset(&gd->linedata[py], 0, sizeof gd->linedata[py]); + struct grid_line *gl = &gd->linedata[py]; + +#ifdef __APPLE__ + assert(gl->cellused <= gl->cellsize); + assert(gl->extdsize == 0 || gl->extddata != NULL); + assert(gl->cellsize == 0 || gl->celldata != NULL); +#endif + + free(gl->celldata); + free(gl->extddata); + memset(gl, 0, sizeof *gl); } /* Free several lines. */ @@ -449,6 +482,8 @@ grid_scroll_history(struct grid *gd, u_int bg) gd->linedata[gd->hsize].time = current_time; gd->hsize++; gd->scroll_added++; + + grid_check_lines(gd); } /* Clear the history. */ @@ -498,6 +533,8 @@ grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower, u_int bg) gd->hscrolled++; gd->hsize++; gd->scroll_added++; + + grid_check_lines(gd); } /* Expand line to fit to cell. */ @@ -759,6 +796,8 @@ grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny, u_int bg) } if (py != 0 && (py < dy || py >= dy + ny)) gd->linedata[py - 1].flags &= ~GRID_LINE_WRAPPED; + + grid_check_lines(gd); } /* Move a group of cells. */ @@ -1248,6 +1287,8 @@ grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy, sy++; dy++; } + + grid_check_lines(dst); } /* Mark line as dead. */ @@ -1544,6 +1585,8 @@ grid_reflow(struct grid *gd, u_int sx) gd->linedata = target->linedata; free(target); gd->scroll_generation++; + + grid_check_lines(gd); } /* Convert to position based on wrapped lines. */