Avoid division by zero in Cube grid-size checks

On a triangular grid, Cube allows either d1 or d2 (but not both) to be
zero, so it's important to check that each one is not zero before
dividing by it.

The crash could be triggered by, for instance "cube t0x2".
This commit is contained in:
Ben Harris
2023-02-13 00:14:22 +00:00
parent da2767a3f9
commit df783b93e3

8
cube.c
View File

@ -567,9 +567,11 @@ static const char *validate_params(const game_params *params, bool full)
* can safely multiply them and compare against the * can safely multiply them and compare against the
* _remaining_ space. * _remaining_ space.
*/ */
if ((params->d1 > INT_MAX / params->d1) || if ((params->d1 > 0 && params->d1 > INT_MAX / params->d1) ||
(params->d2 > (INT_MAX - params->d1*params->d1) / params->d2) || (params->d2 > 0 &&
(params->d1*params->d2 > (INT_MAX - params->d1*params->d1 - params->d2 > (INT_MAX - params->d1*params->d1) / params->d2) ||
(params->d2 > 0 &&
params->d1*params->d2 > (INT_MAX - params->d1*params->d1 -
params->d2*params->d2) / params->d2)) params->d2*params->d2) / params->d2))
return "Grid area must not be unreasonably large"; return "Grid area must not be unreasonably large";
} }