comparison rogue4/state.c @ 83:09db0cf536af

Properly handle invalid room references in savefiles. In all games, rs_write_room_reference() stored -1 for a nonexistent room, but rs_read_room_reference() did not check for out-of-bounds values, leading to pointers to rooms[-1], which sometimes caused crashes. rs_read_room_reference() has now been modified to use NULL instead. Some of the games required further changes to replace NULL with the pointer to the actual room. Others are capable of handling NULL for objects not in any room.
author John "Elwin" Edwards
date Thu, 08 Aug 2013 12:41:35 -0700
parents 9535a08ddc39
children 1b73a8641b37
comparison
equal deleted inserted replaced
82:f11eeafc6568 83:09db0cf536af
1210 { 1210 {
1211 int i; 1211 int i;
1212 1212
1213 rs_read_int(inf, &i); 1213 rs_read_int(inf, &i);
1214 1214
1215 *rp = &rooms[i]; 1215 if (i >= 0 && i < MAXROOMS)
1216 *rp = &rooms[i];
1217 else
1218 *rp = NULL;
1216 1219
1217 return(READSTAT); 1220 return(READSTAT);
1218 } 1221 }
1219 1222
1220 int 1223 int
2058 2061
2059 int 2062 int
2060 rs_restore_file(int inf) 2063 rs_restore_file(int inf)
2061 { 2064 {
2062 bool junk; 2065 bool junk;
2066 THING *mitem;
2063 int endian = 0x01020304; 2067 int endian = 0x01020304;
2064 big_endian = ( *((char *)&endian) == 0x01 ); 2068 big_endian = ( *((char *)&endian) == 0x01 );
2065 2069
2066 rs_read_boolean(inf, &after); 2070 rs_read_boolean(inf, &after);
2067 rs_read_boolean(inf, &noscore); 2071 rs_read_boolean(inf, &noscore);
2154 rs_read_int(inf,&between); /* 5.2-daemons.c */ 2158 rs_read_int(inf,&between); /* 5.2-daemons.c */
2155 rs_read(inf, lvl_mons, sizeof(lvl_mons)); /* 5.2-monsters.c */ 2159 rs_read(inf, lvl_mons, sizeof(lvl_mons)); /* 5.2-monsters.c */
2156 rs_read(inf, wand_mons, sizeof(wand_mons)); /* 5.2-monsters.c */ 2160 rs_read(inf, wand_mons, sizeof(wand_mons)); /* 5.2-monsters.c */
2157 rs_read_coord(inf, &nh); /* 5.2-move.c */ 2161 rs_read_coord(inf, &nh); /* 5.2-move.c */
2158 rs_read_boolean(inf, &got_genocide); /* 5.2-things.c */ 2162 rs_read_boolean(inf, &got_genocide); /* 5.2-things.c */
2159 2163
2160 return(READSTAT); 2164 if (proom == NULL)
2161 } 2165 proom = roomin(&hero);
2166 for (mitem = mlist; mitem != NULL; mitem = mitem->l_next) {
2167 if (mitem->t_room == NULL)
2168 mitem->t_room = roomin(&(mitem->t_pos));
2169 }
2170
2171 return(READSTAT);
2172 }