comparison rogue5/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 f502bf60e6e4
children 7f8f43943b1f
comparison
equal deleted inserted replaced
82:f11eeafc6568 83:09db0cf536af
802 { 802 {
803 int i; 803 int i;
804 804
805 rs_read_int(savef, &i); 805 rs_read_int(savef, &i);
806 806
807 if (!encerror()) 807 if (!encerror()) {
808 *rp = &rooms[i]; 808 if (i >= 0 && i < MAXROOMS)
809 *rp = &rooms[i];
810 else
811 *rp = NULL;
812 }
809 } 813 }
810 814
811 void 815 void
812 rs_write_monsters(FILE *savef, struct monster *m, int cnt) 816 rs_write_monsters(FILE *savef, struct monster *m, int cnt)
813 { 817 {
1382 } 1386 }
1383 1387
1384 int 1388 int
1385 rs_restore_file(FILE *savef) 1389 rs_restore_file(FILE *savef)
1386 { 1390 {
1391 THING *mitem;
1387 encclearerr(); 1392 encclearerr();
1388 1393
1389 rs_read_int(savef, &noscore); 1394 rs_read_int(savef, &noscore);
1390 rs_read_int(savef, &seenstairs); 1395 rs_read_int(savef, &seenstairs);
1391 rs_read_int(savef, &amulet); 1396 rs_read_int(savef, &amulet);
1444 rs_read_daemons(savef, d_list, 20); 1449 rs_read_daemons(savef, d_list, 20);
1445 rs_read_int(savef,&between); 1450 rs_read_int(savef,&between);
1446 rs_read_int(savef,&group); 1451 rs_read_int(savef,&group);
1447 rs_read_window(savef,stdscr); 1452 rs_read_window(savef,stdscr);
1448 1453
1454 if (player.t_room == NULL)
1455 player.t_room = roomin(&hero);
1456 for (mitem = mlist; mitem != NULL; mitem = mitem->l_next) {
1457 if (mitem->t_room == NULL)
1458 mitem->t_room = roomin(&(mitem->t_pos));
1459 }
1460
1449 return( encclearerr() ); 1461 return( encclearerr() );
1450 } 1462 }