annotate rogue3/state.c @ 110:5f51f7d9805f

arogue5: fix some save/restore-related crashes. The save/restore code took the pointer intended as an argument for the doctor() daemon and wrote it to the savefile as an int. I don't know why it took so long to fail horribly. The problem has been avoided by replacing the value with &player when restoring. That seems to be the only argument ever actually used. The code also writes only four bytes for an unsigned long; if sizeof(long) == 8, it casts to unsigned int first. It failed to do the cast when reading back, with the result that four bytes were read and the other half of the number was effectively uninitialized. It apparently works now, but the save/restore code ought still to be regarded as decidedly unfortunate.
author John "Elwin" Edwards
date Mon, 06 Jan 2014 15:57:17 -0500
parents 09db0cf536af
children 7f8f43943b1f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
1 /*
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
2 state.c - Portable Rogue Save State Code
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
3
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
4 Copyright (C) 1999, 2000, 2005, 2007, 2008 Nicholas J. Kisseberth
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
5 All rights reserved.
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
6
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
7 Redistribution and use in source and binary forms, with or without
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
8 modification, are permitted provided that the following conditions
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
9 are met:
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
10 1. Redistributions of source code must retain the above copyright
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
11 notice, this list of conditions and the following disclaimer.
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
12 2. Redistributions in binary form must reproduce the above copyright
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
13 notice, this list of conditions and the following disclaimer in the
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
14 documentation and/or other materials provided with the distribution.
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
15 3. Neither the name(s) of the author(s) nor the names of other contributors
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
16 may be used to endorse or promote products derived from this software
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
17 without specific prior written permission.
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
18
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
22 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
29 SUCH DAMAGE.
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
30 */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
31
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
32 #include <stdlib.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
33 #include <string.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
34 #include <curses.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
35 #include <errno.h>
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
36 #include "rogue.h"
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
37
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
38 /************************************************************************/
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
39 /* Save State Code */
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
40 /************************************************************************/
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
41
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
42 #define RSID_STATS 0xABCD0001
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
43 #define RSID_THING 0xABCD0002
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
44 #define RSID_THING_NULL 0xDEAD0002
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
45 #define RSID_OBJECT 0xABCD0003
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
46 #define RSID_MAGICITEMS 0xABCD0004
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
47 #define RSID_KNOWS 0xABCD0005
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
48 #define RSID_GUESSES 0xABCD0006
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
49 #define RSID_OBJECTLIST 0xABCD0007
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
50 #define RSID_BAGOBJECT 0xABCD0008
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
51 #define RSID_MONSTERLIST 0xABCD0009
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
52 #define RSID_MONSTERSTATS 0xABCD000A
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
53 #define RSID_MONSTERS 0xABCD000B
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
54 #define RSID_TRAP 0xABCD000C
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
55 #define RSID_WINDOW 0xABCD000D
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
56 #define RSID_DAEMONS 0xABCD000E
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
57 #define RSID_IWEAPS 0xABCD000F
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
58 #define RSID_IARMOR 0xABCD0010
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
59 #define RSID_SPELLS 0xABCD0011
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
60 #define RSID_ILIST 0xABCD0012
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
61 #define RSID_HLIST 0xABCD0013
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
62 #define RSID_DEATHTYPE 0xABCD0014
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
63 #define RSID_CTYPES 0XABCD0015
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
64 #define RSID_COORDLIST 0XABCD0016
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
65 #define RSID_ROOMS 0XABCD0017
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
66
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
67 #define READSTAT (format_error || read_error )
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
68 #define WRITESTAT (write_error)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
69
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
70 static int read_error = FALSE;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
71 static int write_error = FALSE;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
72 static int format_error = FALSE;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
73 static int endian = 0x01020304;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
74 #define big_endian ( *((char *)&endian) == 0x01 )
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
75
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
76 void
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
77 rs_write(FILE *savef, const void *ptr, size_t size)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
78 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
79 encwrite(ptr, size, savef);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
80 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
81
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
82 void
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
83 rs_read(FILE *savef, void *ptr, size_t size)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
84 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
85 encread(ptr, size, savef);
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
86 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
87
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
88 void
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
89 rs_write_int(FILE *savef, int c)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
90 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
91 char bytes[4];
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
92 char *buf = (char *) &c;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
93
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
94 if (big_endian)
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
95 {
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
96 bytes[3] = buf[0];
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
97 bytes[2] = buf[1];
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
98 bytes[1] = buf[2];
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
99 bytes[0] = buf[3];
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
100 buf = bytes;
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
101 }
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
102
527e2150eaf0 Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
edwarj4
parents:
diff changeset
103 rs_write(savef, buf, 4);