From 5cac6a09c4db2b7e05c3e8dfd8920e2cdd1b8b03 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 22 Jan 2023 08:54:06 +0000 Subject: [PATCH] Black Box: reject negative ball counts in game_params. You can inject one via a game desc string such as "10x10M5m-1", and it's clearly silly. _Zero_ balls, on the other hand, are a perfectly fine number: there's nothing incoherent about a BB puzzle in which the possible numbers of balls vary from (say) 0 to 5 inclusive, so that part of the challenge is to work out as efficiently as possible whether there are even any balls at all. (We only need to check minballs, because once we know minballs >= 0, the subsequent check ensures that maxballs >= minballs, and hence, by transitivity, maxballs >= 0 too.) --- blackbox.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/blackbox.c b/blackbox.c index c93678a..22ef4cb 100644 --- a/blackbox.c +++ b/blackbox.c @@ -192,6 +192,8 @@ static const char *validate_params(const game_params *params, bool full) * types, and could be worked around if required. */ if (params->w > 255 || params->h > 255) return "Widths and heights greater than 255 are not supported"; + if (params->minballs < 0) + return "Negative number of balls"; if (params->minballs > params->maxballs) return "Minimum number of balls may not be greater than maximum"; if (params->minballs >= params->w * params->h)