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.
This commit is contained in:
Ben Harris
2023-01-10 00:20:36 +00:00
parent ed75535fc2
commit 5cc9bfb811

View File

@ -12,6 +12,7 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <limits.h>
#include <math.h> #include <math.h>
#include "tree234.h" #include "tree234.h"
@ -162,7 +163,9 @@ static void decode_params(game_params *params, char const *string)
params->n = atoi(p); params->n = atoi(p);
while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++; while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++;
} else { } 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) { 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)) if (full && params->unique && (params->w <= 2 || params->h <= 2))
return "Width and height must both be greater than two"; 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) if (params->n < 0)
return "Mine count may not be negative"; return "Mine count may not be negative";
if (params->n > params->w * params->h - 9) if (params->n > params->w * params->h - 9)