2127 Commits

Author SHA1 Message Date
5782e29db4 Tracks: make sure moves are valid in execute_move()
Tracks allowed moves in execute_move() that shouldn't have been allowed,
like changing the state of the edges of the board.  This moves couldn't
be generated by interpret_move(), but could be loaded from a save file.
Now execute_move() uses the same ui_can_flip_*() functions as
interpret_move() to decide whether a particular move is allowed.  This
should prevent some assertion failures when loading corrupted save
files.
2023-01-15 16:24:27 +00:00
15974d06bb Towers: reject descriptions with odd characters at the end
Towers' new_game() causes an assertion failure on game description
strings that contain spurious characters after a valid description, so
validate_desc() should also refuse such a description.  The problem
could be demonstrated by editing the game description in the
"Specific" dialogue box to add a '!' at the end, which caused
"new_game: Assertion `!*p' failed.".
2023-01-15 16:24:27 +00:00
5bd02f982a Mines: No moving once you're dead!
If a Mines save file contains a move after the player has already
died, this can lead to an assertion failure once there are more mines
that covered squares.  Better to just reject any move after the
player's died.
2023-01-15 16:24:27 +00:00
d3290195da Untangle: forbid descriptions that connect a node to itself
These cause an assertion failure in new_game(), so they should be
rejected by validate_desc().
2023-01-15 16:24:27 +00:00
ba944f9f95 Tighten Bridges' validate_desc()
It allowed V, W, X, Y, H, I, J, and K to appear in game descriptions
even though new_game() didn't ascribe any meaning to those letters and
would fail an assertion if they ever occurred.  As far as I can tell,
those letters have never done anything, so I've just removed the
checks for them from validate_desc().
2023-01-15 16:24:27 +00:00
8a06ff26fc Filling: validate length of auto-solve move strings
Without this, execute_move() can end up reading off the end of the
move string, which isn't very friendly.  Also remove the comment
saying that the move string doesn't have to be null-terminated,
because now it does.
2023-01-15 16:24:27 +00:00
a45f1830cf Explain what decode_ui() should do with invalid input
It can't signal an error, but it's worth documenting that it can
receive invalid input and should do what it can with it.  I assume
that failing to decode game_ui data from a newer version generally
won't be disastrous the way failing to decode a description or move
string would be.
2023-01-15 16:24:27 +00:00
8c5279cf75 Same Game: reject moves with unexpected characters in
Previously if a move string starting with "M" contained anything else
other than a digit or a comma, execute_move() would spin trying to
parse it.  Now it returns NULL.
2023-01-15 16:24:27 +00:00
0dbbd52935 Palisade: remove assertion from decode_ui()
Other games tolerate receiving an encoded game_ui even if they can
never generate one.  This is sensible, since it means that if a new
version starts saving UI state, old versions can load save files
generated by those newer versions.
2023-01-15 16:24:27 +00:00
e616d7aac9 Mosaic: fault out-of-bounds moves in execute_move()
Returning NULL in this case is better than dereferencing it.
2023-01-15 16:24:27 +00:00
68f9fae973 When loading, don't decode_ui unless we have a UI
If the save file doesn't have a UI line, it's not sensible to try to
decode it.
2023-01-15 16:24:27 +00:00
e5d106eb27 Don't allow negative clues in Pattern 2023-01-15 16:24:27 +00:00
38cf1955e5 Palisade: don't leak memory on a bad move
Invalid moves can turn up in corrupted save files, and puzzles
shouldn't leak memory when failing to load a corrupted save file.
2023-01-15 16:24:27 +00:00
c2eedeedfe Black Box: correct order of validation checks for "F" commands
It doesn't do much good to range-check an argument after using it as
an array index.
2023-01-15 16:24:27 +00:00
d5b8a20def Last-ditch point-count limit for Untangle
Anything over INT_MAX/3 will cause an integer overflow, so put the
limit there for now.
2023-01-15 16:24:27 +00:00
85ccdf2f75 Adjust Undead upper grid-size limit to avoid overflow 2023-01-15 16:24:27 +00:00
51dcf4add6 Last-ditch maximum size limit for Twiddle
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
c53e0d3867 Last-ditch maximum size limit for Tracks
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
07999443c2 Limit size of puzzle in Tents to avoid integer overflow 2023-01-15 16:24:27 +00:00
91d96fa0bc Last-ditch maximum size limit for Sixteen
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
5c36e1536a Last-ditch maximum size limit for Signpost
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
d5ec2758ee Last-ditch maximum size limit for Same Game
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
b090c82df1 Also limit Pegs to at least 1x1 even when not doing full validation 2023-01-15 16:24:27 +00:00
6e40605f1e Last-ditch maximum size limit for Pegs
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
8a3fb82e23 Last-ditch maximum size limit for Pearl
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
91c0fac1dc Last-ditch maximum size limit for Palisade
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
dd00e9c532 Integer overflow protection in Pattern
Both for grid sizes and for clue values.
2023-01-15 16:24:27 +00:00
40ec3aaf09 Last-ditch maximum size limit for Netslide
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
051357bb24 Last-ditch maximum size limit for Net
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
48e3452264 Avoid integer overflow in Mosaic maximum-size check 2023-01-15 16:24:27 +00:00
9e2e0692ed Also check for tiny grids in Mines
A zero-size grid isn't acceptable even if someone has generated it for
us.
2023-01-15 16:24:27 +00:00
5cc9bfb811 Last-ditch maximum size limit for Mines
This makes sure that width * height <= INT_MAX, which it rather needs
to be.  Also a similar check in decode_params when defaulting the
number of mines.
2023-01-15 16:24:27 +00:00
ed75535fc2 Last-ditch maximum size limit for Map
This makes sure that width * height <= INT_MAX, which it rather needs
to be.  Also a similar check in decode_params when defaulting the
number of regions.
2023-01-15 16:24:27 +00:00
261a9568fa Last-ditch maximum size limit for Magnets
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
d71bba1a17 Limit maximum grid size in Loopy
Every grid shape has its own limit, so this involved adding a new
interface between loopy.c and grid.c.  The limits are based on ensuring
that the co-ordinate system of the grid doesn't overflow INT_MAX and
neither do the lengths of the face and dot arrays.

Though now I come to look at it I think the actual limits of grid.c are
much lower.  Hmm.
2023-01-15 16:24:27 +00:00
fcda12f4b7 Last-ditch maximum size limit for Light Up
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
98724b9093 Last-ditch grid-size limit for Inertia
At least prevent integer overflow when constructing the grid.
2023-01-15 16:24:27 +00:00
d60192531e Insist that Flood grids must have non-zero size 2023-01-15 16:24:27 +00:00
da220a77d1 Last-ditch grid-size limit for Flood
At least prevent integer overflow when constructing the grid.
2023-01-15 16:24:27 +00:00
26d0633f87 Last-ditch maximum size limit for Flip
This makes sure that width * height <= INT_MAX, which it rather needs
to be.  Also in Flip's case that the square of the area still fits in
an int.
2023-01-15 16:24:27 +00:00
522588f699 Last-ditch grid-size limit for Fifteen
At least prevent integer overflow when constructing the grid.
2023-01-15 16:24:27 +00:00
d422dd6009 Last-ditch grid-size limit for Galaxies
At least prevent integer overflow when constructing the grid.
2023-01-15 16:24:27 +00:00
b3f3345764 Last-ditch grid-size limit for Dominosa
At least prevent integer overflow when constructing the grid.
2023-01-15 16:24:27 +00:00
97484b098f Last-ditch maximum size limit for Bridges
This makes sure that width * height <= INT_MAX, which it rather needs
to be.
2023-01-15 16:24:27 +00:00
21193eaf93 Palisade: forbid moves that remove grid edges
Without this check, a corrupt save file can include a move that
removes an edge of the grid, and then is_solved() walks off the edge
of the grid causing a buffer over- or under-run.

To demonstrate the bug, load this save file in a build with
AddressSanitizer:

SAVEFILE:41:Simon Tatham's Portable Puzzle Collection
VERSION :1:1
GAME    :8:Palisade
PARAMS  :5:5x5n5
CPARAMS :5:5x5n5
DESC    :0:
NSTATES :1:2
STATEPOS:1:2
MOVE    :6:F0,0,1
2023-01-15 16:24:27 +00:00
b3d4a41979 Don't load too many states just because there's no STATEPOS
If we start seeing state records in a save file (MOVE, SOLVE, or
RESTART), we should already have seen STATEPOS, so emit an error if not.
This avoids the situation where we overrun the end of the state array
because we're continuing loading states in the hope a STATEPOS will come
along.  I've also added an assertion that we're not overrunning the
state array for added paranoia.

An earlier version of this fix just removed the test for data.statepos
at the head of the loop, but that's wrong for a file that only has the
initial state.

This bug can be demonstrated by building Bridges with AddressSanitizer
and loading this save file:

SAVEFILE:41:Simon Tatham's Portable Puzzle Collection
VERSION :1:1
GAME    :7:Bridges
PARAMS  :13:7x7i30e10m2d0
CPARAMS :13:7x7i30e10m2d0
DESC    :24:a4b4a1g1a2a8a4a4m2b2b3e3
NSTATES :1:2
MOVE    :10:L1,0,4,0,1
MOVE    :10:L1,0,4,0,2
2023-01-15 16:21:37 +00:00
e5717d1ba2 Range-check record lengths when deserialising games
"1999999999999999999999999999999999999999999999999999" as a record
length should lead to an error, not a buffer overrun.

(fun fact that was less obvious to me than it should have been: very
large powers of ten are multiples of large powers of two, so that number
is -1 mod 2^32)

This bug can be demonstrated by building any puzzle with
AddressSanitizer and then loading this save file:

SAVEFILE:41:Simon Tatham's Portable Puzzle Collection
VERSION :1999999999999999999999999999999999999999999999999999:1
2023-01-15 16:21:37 +00:00
942d883d9b Range-check normal moves in Undead
Normal moves shouldn't be allowed to write outside the board.  This
buffer overrun can be demonstrated by building Undead with
AddressSanitizer and loading this save file:

SAVEFILE:41:Simon Tatham's Portable Puzzle Collection
VERSION :1:1
GAME    :6:Undead
PARAMS  :5:4x4dn
CPARAMS :5:4x4dn
DESC    :48:5,0,5,cRRaLRcLRc,0,2,1,3,1,0,0,3,4,3,2,3,4,2,1,1
NSTATES :1:2
STATEPOS:1:2
MOVE    :3:Z10
2023-01-15 16:21:37 +00:00
4845f3e913 Correct RANGECHECK macro in Black Box
Lasers are numbered from 0 to nlasers-1 inclusive, so the upper limit
should be "<", not "<=".
2023-01-15 16:21:37 +00:00
952ef8ca56 Undead: fix buffer overrun in "M" command
The guessable squares are numbered up to num_total, not "wh".  The
latter includes mirror squares that aren't included in the various
arrays describing the game state.

To reproduce the problem, build Undead with AddressSanitizer and press
"M".
2023-01-15 16:21:37 +00:00