From a98ac4bb428ab5c1ff665aa1def6cc14d02a4e19 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sat, 28 Jan 2023 00:45:38 +0000 Subject: [PATCH] Don't allow Bridges games with < 2 islands Technically, a game with no islands is always solved, but it causes a null-pointer dereference at startup because there's nowhere to put the cursor. Games with one island are always insoluble because the island must have at least one bridge and there's nowhere for it to go. So the minimum playable game has two islands. To demonstrate the segfault, try loading this save file: SAVEFILE:41:Simon Tatham's Portable Puzzle Collection VERSION :1:1 GAME :7:Bridges PARAMS :1:3 CPARAMS :1:3 DESC :1:i NSTATES :1:1 STATEPOS:1:1 --- bridges.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bridges.c b/bridges.c index 35cdcca..510af44 100644 --- a/bridges.c +++ b/bridges.c @@ -2007,15 +2007,15 @@ generated: static const char *validate_desc(const game_params *params, const char *desc) { - int i, wh = params->w * params->h; + int i, wh = params->w * params->h, nislands = 0; for (i = 0; i < wh; i++) { if (*desc >= '1' && *desc <= '9') - /* OK */; + nislands++; else if (*desc >= 'a' && *desc <= 'z') i += *desc - 'a'; /* plus the i++ */ else if (*desc >= 'A' && *desc <= 'G') - /* OK */; + nislands++; else if (!*desc) return "Game description shorter than expected"; else @@ -2024,6 +2024,8 @@ static const char *validate_desc(const game_params *params, const char *desc) } if (*desc || i > wh) return "Game description longer than expected"; + if (nislands < 2) + return "Game description has too few islands"; return NULL; }