X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=saveresume.c;h=ecfc643d8f772ad82581b5cf9bcaa688f710c0c4;hb=f0dc3d3b7c8c169ae0581b7e9c5e209dd28c682a;hp=088bab150a17e62f561becd19b5151d4880fa19a;hpb=d23111dabab46a8bcdf7e453ec099e4521d1cad6;p=open-adventure.git diff --git a/saveresume.c b/saveresume.c index 088bab1..ecfc643 100644 --- a/saveresume.c +++ b/saveresume.c @@ -11,7 +11,7 @@ * see the history.adoc file in the source distribution for discussion. */ -#define VRSION 26 /* bump on save format change */ +#define VRSION 27 /* bump on save format change */ /* * If you change the first three members, the resume function may not properly @@ -36,7 +36,7 @@ int savefile(FILE *fp, long version) save.mode = -1; save.version = (version == 0) ? VRSION : version; - memcpy(&save.game, &game, sizeof(struct game_t)); + save.game = game; IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp)); return (0); } @@ -85,7 +85,8 @@ int resume(void) #endif FILE *fp = NULL; - if (game.loc != 1 || game.abbrev[1] != 1) { + if (game.loc != 1 || + game.abbrev[1] != 1) { rspeak(RESUME_ABANDON); if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN])) return GO_CLEAROBJ; @@ -104,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 @@ -117,10 +120,46 @@ 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 { - memcpy(&game, &save.game, sizeof(struct game_t)); + } 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; + } + /* Bounds check for location arrays + */ + for (int i = 0; i <= NDWARVES; i++) { + if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS || + valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) { + return false; + } + } + + for (int i = 0; i <= NOBJECTS; i++) { + if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS || + valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) { + return false; + } + } + return true; +} + +/* end */ \ No newline at end of file