From 58cf204eba055cc7633aa83be7ec6f7c06ed70b7 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 6 Apr 2023 19:03:02 -0400 Subject: [PATCH] Implement the magic-cookie check and its test. --- adventure.yaml | 1 + saveresume.c | 4 +++- tests/badmagic.chk | 22 ++++++++++++++++++++++ tests/badmagic.log | 8 ++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/badmagic.chk create mode 100644 tests/badmagic.log diff --git a/adventure.yaml b/adventure.yaml index a48ee7d..8909914 100644 --- a/adventure.yaml +++ b/adventure.yaml @@ -3186,6 +3186,7 @@ arbitrary_messages: !!omap # %d of %d "random" messages %d of %d "class" messages # %d of %d hints %d of %d turn thresholds' - RESUME_ABANDON: 'To resume an earlier Adventure, you must abandon the current one.' +- BAD_SAVE: 'Oops, that does not look like a valid save file.' - VERSION_SKEW: |- I'm sorry, but that Adventure was begun using Version %d.%d of the save file format, and this program uses Version %d.%d. You must find an instance diff --git a/saveresume.c b/saveresume.c index 76de1c4..0ed8cd3 100644 --- a/saveresume.c +++ b/saveresume.c @@ -135,7 +135,9 @@ int restore(FILE* fp) IGNORE(fread(&save, sizeof(struct save_t), 1, fp)); fclose(fp); - if (save.version != SAVE_VERSION) { + if (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0) + rspeak(BAD_SAVE); + else if (save.version != SAVE_VERSION) { rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), SAVE_VERSION / 10, MOD(SAVE_VERSION, 10)); } else if (!is_valid(save.game)) { rspeak(SAVE_TAMPERING); diff --git a/tests/badmagic.chk b/tests/badmagic.chk new file mode 100644 index 0000000..08c8ffe --- /dev/null +++ b/tests/badmagic.chk @@ -0,0 +1,22 @@ + +Welcome to Adventure!! Would you like instructions? + +> n + +You are standing at the end of a road before a small brick building. +Around you is a forest. A small stream flows out of the building and +down a gully. + +> resume +Can't open file y, try again. + +Oops, that does not look like a valid save file. + +You're in front of building. + +> +You scored 32 out of a possible 430, using 1 turn. + +You are obviously a rank amateur. Better luck next time. + +To achieve the next higher rating, you need 14 more points. diff --git a/tests/badmagic.log b/tests/badmagic.log new file mode 100644 index 0000000..4ee9f10 --- /dev/null +++ b/tests/badmagic.log @@ -0,0 +1,8 @@ +## Resume from filename withoy the right magic at the front +# SPDX-FileCopyrightText: Eric S. Raymond +# SPDX-License-Identifier: BSD-2-Clause +#NOCOMPARE advent430 doesn't have this test +n +resume +y +../main.o -- 2.31.1