shithub: puzzles

Download patch

ref: a02c55b0492453ea7ca4e4ae63cb90ba4c93a3a5
parent: 023ce7554c19dcf6f4432407b9eedb850acc7289
author: Ben Harris <bjh21@bjh21.me.uk>
date: Sat Jan 7 18:31:25 EST 2023

Undead: check for valid commands in execute_move()

Previously, Undead's execute_move would go into a spin when it
encountered an unexpected command character in a move string.  Now it
rejects the move instead.

--- a/undead.c
+++ b/undead.c
@@ -2080,9 +2080,8 @@
         if (c == 'S') {
             move++;
             solver = true;
-        }
-        if (c == 'G' || c == 'V' || c == 'Z' || c == 'E' ||
-            c == 'g' || c == 'v' || c == 'z') {
+        } else if (c == 'G' || c == 'V' || c == 'Z' || c == 'E' ||
+                   c == 'g' || c == 'v' || c == 'z') {
             move++;
             sscanf(move, "%d%n", &x, &n);
             if (c == 'G') ret->guess[x] = 1;
@@ -2093,13 +2092,11 @@
             if (c == 'v') ret->pencils[x] ^= 2;
             if (c == 'z') ret->pencils[x] ^= 4;
             move += n;
-        }
-        if (c == 'D' && sscanf(move + 1, "%d,%d%n", &x, &y, &n) == 2 &&
-            is_clue(ret, x, y)) {
+        } else if (c == 'D' && sscanf(move + 1, "%d,%d%n", &x, &y, &n) == 2 &&
+                   is_clue(ret, x, y)) {
             ret->hints_done[clue_index(ret, x, y)] ^= 1;
             move += n + 1;
-        }
-        if (c == 'M') {
+        } else if (c == 'M') {
             /*
              * Fill in absolutely all pencil marks in unfilled
              * squares, for those who like to play by the rigorous
@@ -2110,6 +2107,10 @@
                 if (ret->guess[i] == 7)
                     ret->pencils[i] = 7;
             move++;
+        } else {
+            /* Unknown move type. */
+            free_game(ret);
+            return NULL;
         }
         if (*move == ';') move++;
     }