Implement the magic-cookie check and its test.

This commit is contained in:
Eric S. Raymond 2023-04-06 19:03:02 -04:00
parent d16822a583
commit 58cf204eba
4 changed files with 34 additions and 1 deletions

View file

@ -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

View file

@ -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);

22
tests/badmagic.chk Normal file
View file

@ -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.

8
tests/badmagic.log Normal file
View file

@ -0,0 +1,8 @@
## Resume from filename withoy the right magic at the front
# SPDX-FileCopyrightText: Eric S. Raymond <esr@thyrsus.com>
# SPDX-License-Identifier: BSD-2-Clause
#NOCOMPARE advent430 doesn't have this test
n
resume
y
../main.o