shithub: puzzles

Download patch

ref: a2212e82aa2f4b9a4ee22783d6fed2761c213432
parent: d246077e78bb1aeafe8829927db23f281cd03c72
author: Ben Harris <[email protected]>
date: Mon Jan 2 12:44:04 EST 2023

Use a dynamically-sized buffer for Pattern row clues

--- a/pattern.c
+++ b/pattern.c
@@ -1275,6 +1275,7 @@
     int tilesize;
     unsigned char *visible, *numcolours;
     int cur_x, cur_y;
+    char *strbuf; /* Used for formatting clues. */
 };
 
 static char *interpret_move(const game_state *state, game_ui *ui,
@@ -1720,6 +1721,8 @@
     ds->numcolours = snewn(ds->w + ds->h, unsigned char);
     memset(ds->numcolours, 255, ds->w + ds->h);
     ds->cur_x = ds->cur_y = 0;
+    ds->strbuf = snewn(state->common->rowsize *
+                       MAX_DIGITS(*state->common->rowdata) + 1, char);
 
     return ds;
 }
@@ -1727,6 +1730,7 @@
 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
 {
     sfree(ds->visible);
+    sfree(ds->strbuf);
     sfree(ds);
 }
 
@@ -1823,7 +1827,7 @@
     if (i < state->common->w) {
         for (j = 0; j < rowlen; j++) {
             int x, y;
-            char str[80];
+            char str[MAX_DIGITS(*rowdata) + 1];
 
             x = rx;
             y = BORDER + TILE_SIZE * (TLBORDER(state->common->h)-1);
@@ -1834,17 +1838,15 @@
         }
     } else {
         int x, y;
-        char str[280];
         size_t off = 0;
 
-        for (j = 0; j < rowlen; j++) {
-            assert(off < 260);
-            off += sprintf(str + off, "%s%d", j ? "  " : "", rowdata[j]);
-        }
+        assert(rowlen <= state->common->rowsize);
+        for (j = 0; j < rowlen; j++)
+            off += sprintf(ds->strbuf + off, "%s%d", j ? "  " : "", rowdata[j]);
         y = ry;
         x = BORDER + TILE_SIZE * (TLBORDER(state->common->w)-1);
         draw_text(dr, x+TILE_SIZE, y+TILE_SIZE/2, FONT_VARIABLE,
-                  fontsize, ALIGN_HRIGHT | ALIGN_VCENTRE, colour, str);
+                  fontsize, ALIGN_HRIGHT | ALIGN_VCENTRE, colour, ds->strbuf);
     }
 
     unclip(dr);