aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Harris <bjh21@bjh21.me.uk>2023-02-15 14:07:34 +0000
committerBen Harris <bjh21@bjh21.me.uk>2023-02-15 14:07:59 +0000
commit9394e9c74bdb48dc1c74693bcb41fd35f8fc743c (patch)
treed6d32eacd427f2d14655f73ad23c6ffb6bf7efed
parent7364ce8e266d947be146d635958a7b282752aac6 (diff)
downloadpuzzles-9394e9c74bdb48dc1c74693bcb41fd35f8fc743c.zip
puzzles-9394e9c74bdb48dc1c74693bcb41fd35f8fc743c.tar.gz
puzzles-9394e9c74bdb48dc1c74693bcb41fd35f8fc743c.tar.bz2
puzzles-9394e9c74bdb48dc1c74693bcb41fd35f8fc743c.tar.xz
Tighten grid-size limit in Mines
Mines uses random_upto() to decide where to place mines, and random_upto() takes a maximum limit of 2^28-1, so limit the number of grid squares to that (or INT_MAX if someone's still trying to build on a 16-bit system). This avoids an assertion failure: "random_upto: Assertion `bits < 32' failed." which can be demonstrated by this save file: SAVEFILE:41:Simon Tatham's Portable Puzzle Collection VERSION :1:1 GAME :5:Mines PARAMS :5:18090 CPARAMS :5:18090 DESC :11:r9,u,MEdff6 UI :2:D0 TIME :1:0 NSTATES :1:2 STATEPOS:1:2 MOVE :4:O2,1
-rw-r--r--mines.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/mines.c b/mines.c
index 04364e5..7ab4dbc 100644
--- a/mines.c
+++ b/mines.c
@@ -265,7 +265,14 @@ static const char *validate_params(const game_params *params, bool full)
return "Width and height must both be at least one";
if (params->w > SHRT_MAX || params->h > SHRT_MAX)
return "Neither width nor height may be unreasonably large";
+ /*
+ * We use random_upto() to place mines, and its maximum limit is 2^28-1.
+ */
+#if (1<<28)-1 < INT_MAX
+ if (params->w > ((1<<28)-1) / params->h)
+#else
if (params->w > INT_MAX / params->h)
+#endif
return "Width times height must not be unreasonably large";
if (params->n < 0)
return "Mine count may not be negative";