Make sure that moves in Flood use only valid colours

If execute_move() receieves a move that uses a colour beyond the range
for the current game, it now rejects it.  Without this a solve string
containing an invalid colour would cause an assertion failure: "fill:
Assertion `oldcolour != newcolour' failed."  While I was in the area I
put a range check on colours for normal moves as well.  To demonstrate
the problem, load this save file:

SAVEFILE:41:Simon Tatham's Portable Puzzle Collection
VERSION :1:1
GAME    :5:Flood
PARAMS  :7:6x6c6m5
CPARAMS :7:6x6c6m3
DESC    :39:432242034203340350204502505323231342,17
NSTATES :1:2
STATEPOS:1:2
MOVE    :2:S6
This commit is contained in:
Ben Harris
2023-02-14 22:02:35 +00:00
parent 5a0a2b9166
commit 7364ce8e26

10
flood.c
View File

@ -886,7 +886,7 @@ static game_state *execute_move(const game_state *state, const char *move)
if (move[0] == 'M' && if (move[0] == 'M' &&
sscanf(move+1, "%d", &c) == 1 && sscanf(move+1, "%d", &c) == 1 &&
c >= 0 && c >= 0 && c < state->colours &&
c != state->grid[FILLY * state->w + FILLX] && c != state->grid[FILLY * state->w + FILLX] &&
!state->complete) { !state->complete) {
int *queue = snewn(state->w * state->h, int); int *queue = snewn(state->w * state->h, int);
@ -945,10 +945,12 @@ static game_state *execute_move(const game_state *state, const char *move)
return NULL; return NULL;
}; };
sol->moves[i] = atoi(p); sol->moves[i] = atoi(p);
if (i == 0 ? if (sol->moves[i] < 0 || sol->moves[i] >= state->colours ||
(i == 0 ?
sol->moves[i] == state->grid[FILLY * state->w + FILLX] : sol->moves[i] == state->grid[FILLY * state->w + FILLX] :
sol->moves[i] == sol->moves[i-1]) sol->moves[i] == sol->moves[i-1]))
/* Solution contains a fill with the current colour. */ /* Solution contains a fill with an invalid colour or
* the current colour. */
goto badsolve; goto badsolve;
p += strspn(p, "0123456789"); p += strspn(p, "0123456789");
if (*p) { if (*p) {