From 36f72f190259b3ff72a87d1e888a578d6fd5b8da Mon Sep 17 00:00:00 2001 From: NHOrus Date: Sun, 13 Aug 2017 12:41:42 +0100 Subject: [PATCH] Validating sanity of locations in restored game --- saveresume.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/saveresume.c b/saveresume.c index d854174..8897945 100644 --- a/saveresume.c +++ b/saveresume.c @@ -105,6 +105,8 @@ int resume(void) return restore(fp); } +bool is_valid(struct game_t); + int restore(FILE* fp) { /* Read and restore game state from file, assuming @@ -118,10 +120,32 @@ int restore(FILE* fp) fclose(fp); if (save.version != VRSION) { rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10)); - } else { - game = save.game; + } else if (is_valid(save.game)) { + game = save.game; } return GO_TOP; } -/* end */ +bool is_valid(struct game_t valgame) +{ + /* Save files can be roughly grouped into three groups: + * With valid, reaceable state, with valid, but unreachable + * state and with invaild state. We check that state is + * valid: no states are outside minimal or maximal value + */ + + /* Bounds check for locations + */ + if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS || + valgame.chloc < -1 || valgame.chloc > NLOCATIONS || + valgame.loc < -1 || valgame.loc > NLOCATIONS || + valgame.newloc < -1 || valgame.newloc > NLOCATIONS || + valgame.oldloc < -1 || valgame.oldloc > NLOCATIONS || + valgame.oldloc < -1 || valgame.oldloc > NLOCATIONS) { + return false; + } + + return true; +} + +/* end */ \ No newline at end of file -- 2.31.1