From c39a070f13c19c2cf30bb961426bea8c5140c9a5 Mon Sep 17 00:00:00 2001 From: haranp Date: Wed, 19 Sep 2007 23:28:26 +0000 Subject: Fix for memory corruption when reading savefiles. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@2152 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/files.cc | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'crawl-ref/source/files.cc') diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index 7e2cf37bed..20a0a93579 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -33,6 +33,7 @@ #include #include +#include #ifdef DOS #include @@ -175,12 +176,10 @@ bool is_packed_save(const std::string &name) } #endif -// Returns a full player struct read from the save. -player read_character_info(const std::string &savefile) +// Return the save_info from the save. +player_save_info read_character_info(const std::string &savefile) { - player fromfile; - player backup; - + player_save_info fromfile; FILE *charf = fopen(savefile.c_str(), "rb"); if (!charf) return fromfile; @@ -188,13 +187,16 @@ player read_character_info(const std::string &savefile) char majorVersion = 0; char minorVersion = 0; - backup.copy_from(you); if (determine_version(charf, majorVersion, minorVersion) && majorVersion == SAVE_MAJOR_VERSION) { + // backup before we clobber "you" + const player backup(you); + restore_tagged_file(charf, TAGTYPE_PLAYER_NAME, minorVersion); - fromfile.copy_from(you); - you .copy_from(backup); + + fromfile = you; + you.copy_from(backup); } fclose(charf); @@ -450,9 +452,10 @@ std::string get_savedir_path(const std::string &shortpath) * Returns a list of the names of characters that are already saved for the * current user. */ -std::vector find_saved_characters() + +std::vector find_saved_characters() { - std::vector chars; + std::vector chars; #ifndef DISABLE_SAVEGAME_LISTS std::string searchpath = Options.save_dir; @@ -460,7 +463,7 @@ std::vector find_saved_characters() searchpath = "."; std::vector allfiles = get_dir_files(searchpath); - for (int i = 0, size = allfiles.size(); i < size; ++i) + for (unsigned int i = 0; i < allfiles.size(); ++i) { std::string filename = allfiles[i]; #ifdef LOAD_UNPACKAGE_CMD @@ -490,8 +493,8 @@ std::vector find_saved_characters() #endif if (is_save_file_name(filename)) { - player p = read_character_info(get_savedir_path(filename)); - if (p.is_valid()) + player_save_info p=read_character_info(get_savedir_path(filename)); + if (!p.name.empty()) chars.push_back(p); } @@ -501,7 +504,7 @@ std::vector find_saved_characters() #endif } - std::sort(chars.begin(), chars.end()); + std::sort(chars.rbegin(), chars.rend()); #endif // !DISABLE_SAVEGAME_LISTS return (chars); } -- cgit v1.2.3-54-g00ecf