aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Harris <bjh21@bjh21.me.uk>2023-01-10 00:20:36 +0000
committerBen Harris <bjh21@bjh21.me.uk>2023-01-15 16:24:27 +0000
commit5cc9bfb811854b66c4a570e8100b8a1aad037f0e (patch)
treeb2e7c6bc863ed406c793c799a856b2bf3d7351ff
parented75535fc24217c51f900d42385309c8c8b36cc3 (diff)
downloadpuzzles-5cc9bfb811854b66c4a570e8100b8a1aad037f0e.zip
puzzles-5cc9bfb811854b66c4a570e8100b8a1aad037f0e.tar.gz
puzzles-5cc9bfb811854b66c4a570e8100b8a1aad037f0e.tar.bz2
puzzles-5cc9bfb811854b66c4a570e8100b8a1aad037f0e.tar.xz
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.
-rw-r--r--mines.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/mines.c b/mines.c
index 22332ba..c933f49 100644
--- a/mines.c
+++ b/mines.c
@@ -12,6 +12,7 @@
#include <string.h>
#include <assert.h>
#include <ctype.h>
+#include <limits.h>
#include <math.h>
#include "tree234.h"
@@ -162,7 +163,9 @@ static void decode_params(game_params *params, char const *string)
params->n = atoi(p);
while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++;
} else {
- params->n = params->w * params->h / 10;
+ if (params->h > 0 && params->w > 0 &&
+ params->w <= INT_MAX / params->h)
+ params->n = params->w * params->h / 10;
}
while (*p) {
@@ -258,6 +261,8 @@ static const char *validate_params(const game_params *params, bool full)
*/
if (full && params->unique && (params->w <= 2 || params->h <= 2))
return "Width and height must both be greater than two";
+ if (params->w > INT_MAX / params->h)
+ return "Width times height must not be unreasonably large";
if (params->n < 0)
return "Mine count may not be negative";
if (params->n > params->w * params->h - 9)