1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Rewritten CLoadFile::openNextFile to be cleaner and not leak. Desperate messing around for #989.

This commit is contained in:
Michał W. Urbańczyk 2012-06-08 00:12:31 +00:00
parent 9fc459b5c8
commit e7d82a9702
4 changed files with 82 additions and 59 deletions

View File

@ -349,6 +349,9 @@ namespace range = boost::range;
template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N]; template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
#define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr))) #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
#define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
//XXX pls dont - 'debug macros' are usually more trouble than it's worth //XXX pls dont - 'debug macros' are usually more trouble than it's worth
#define HANDLE_EXCEPTION \ #define HANDLE_EXCEPTION \
catch (const std::exception& e) { \ catch (const std::exception& e) { \

View File

@ -1062,18 +1062,16 @@ void SelectionTab::parseMaps(std::vector<FileInfo> &files, int start, int thread
void SelectionTab::parseGames(std::vector<FileInfo> &files, bool multi) void SelectionTab::parseGames(std::vector<FileInfo> &files, bool multi)
{ {
for(int i=0; i<files.size(); i++) for(int i=0; i<files.size(); i++)
{
try
{ {
CLoadFile lf(files[i].name); CLoadFile lf(files[i].name);
if(!lf.sfile)
continue;
ui8 sign[8]; ui8 sign[8];
lf >> sign; lf >> sign;
if(std::memcmp(sign,"VCMISVG",7)) if(std::memcmp(sign,"VCMISVG",7))
{ throw std::runtime_error("not a correct savefile!");
tlog1 << files[i].name << " is not a correct savefile!" << std::endl;
continue;
}
allItems[i].mapHeader = new CMapHeader(); allItems[i].mapHeader = new CMapHeader();
lf >> *(allItems[i].mapHeader) >> allItems[i].scenarioOpts; lf >> *(allItems[i].mapHeader) >> allItems[i].scenarioOpts;
allItems[i].filename = files[i].name; allItems[i].filename = files[i].name;
@ -1082,8 +1080,13 @@ void SelectionTab::parseGames(std::vector<FileInfo> &files, bool multi)
if((allItems[i].actualHumanPlayers > 1) != multi) //if multi mode then only multi games, otherwise single if((allItems[i].actualHumanPlayers > 1) != multi) //if multi mode then only multi games, otherwise single
{ {
delete allItems[i].mapHeader; vstd::clear_pointer(allItems[i].mapHeader);
allItems[i].mapHeader = NULL; }
}
catch(std::exception &e)
{
vstd::clear_pointer(allItems[i].mapHeader);
tlog3 << "Failed to process " << files[i].name <<": " << e.what() << std::endl;
} }
} }
} }

View File

@ -302,53 +302,55 @@ CLoadFile::~CLoadFile()
int CLoadFile::read( const void * data, unsigned size ) int CLoadFile::read( const void * data, unsigned size )
{ {
char *bytePtr = (char *)data; sfile->read((char *)data,size);
sfile->read(bytePtr, size);
return size; return size;
} }
void CLoadFile::openNextFile(const std::string &fname, int minimalVersion) void CLoadFile::openNextFile(const std::string &fname, int minimalVersion)
{
assert(!reverseEndianess);
assert(minimalVersion <= version);
try
{ {
fName = fname; fName = fname;
sfile = make_unique<std::ifstream>(fname.c_str(),std::ios::binary); sfile = make_unique<std::ifstream>(fname, std::ios::binary);
sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
if(!(*sfile)) if(!(*sfile))
{ THROW_FORMAT("Error: cannot open to read %s!", fname);
tlog1 << "Error: cannot open to read " << fname << std::endl;
sfile.release(); //we can read
}
else
{
char buffer[4]; char buffer[4];
sfile->read(buffer, 4); sfile->read(buffer, 4);
if(std::memcmp(buffer,"VCMI",4)) if(std::memcmp(buffer,"VCMI",4))
{ THROW_FORMAT("Error: not a VCMI file(%s)!", fname);
tlog1 << "Error: not a VCMI file! ( " << fname << " )\n";
sfile.release();
return;
}
*this >> myVersion; *this >> fileVersion;
if(myVersion < minimalVersion) if(fileVersion < minimalVersion)
THROW_FORMAT("Error: too old file format (%s)!", fname);
if(fileVersion > version)
{ {
tlog1 << "Error: Too old file format! (file " << fname << " )\n"; tlog3 << boost::format("Warning format version mismatch: found %d when current is %d! (file %s)\n") % fileVersion % version % fname;
sfile.release();
} auto versionptr = (char*)&fileVersion;
if(myVersion > version)
{
auto versionptr = (char*)&myVersion;
std::reverse(versionptr, versionptr + 4); std::reverse(versionptr, versionptr + 4);
if(myVersion == version) tlog3 << "Version number reversed is " << fileVersion << ", checking...\n";
if(fileVersion == version)
{ {
tlog3 << fname << " seems to have different endianess! Entering reversing mode.\n";
reverseEndianess = true; reverseEndianess = true;
tlog3 << fname << " seems to have different endianess!\n";
} }
else else
THROW_FORMAT("Error: too new file format (%s)!", fname);
}
}
catch(...)
{ {
tlog1 << "Error: Too new file format! (file " << fname << " )\n"; clear(); //if anything went wrong, we delete file and rethrow
sfile.release(); throw;
}
}
} }
} }
@ -361,6 +363,13 @@ void CLoadFile::reportState(CLogger &out)
} }
} }
void CLoadFile::clear()
{
sfile = nullptr;
fName.clear();
fileVersion = 0;
}
CTypeList::CTypeList() CTypeList::CTypeList()
{ {
registerTypes(*this); registerTypes(*this);

View File

@ -686,7 +686,7 @@ template <typename Serializer> class DLL_LINKAGE CISer : public CLoaderBase
public: public:
bool saving; bool saving;
std::map<ui16,CBasicPointerLoader*> loaders; // typeID => CPointerSaver<serializer,type> std::map<ui16,CBasicPointerLoader*> loaders; // typeID => CPointerSaver<serializer,type>
ui32 myVersion; ui32 fileVersion;
bool reverseEndianess; //if source has different endianess than us, we reverse bytes bool reverseEndianess; //if source has different endianess than us, we reverse bytes
std::map<ui32, void*> loadedPointers; std::map<ui32, void*> loadedPointers;
@ -695,7 +695,7 @@ public:
CISer() CISer()
{ {
saving = false; saving = false;
myVersion = version; fileVersion = 0;
smartPointerSerialization = true; smartPointerSerialization = true;
reverseEndianess = false; reverseEndianess = false;
} }
@ -760,13 +760,19 @@ public:
template <typename T> template <typename T>
void loadPrimitive(T &data) void loadPrimitive(T &data)
{ {
char * dataPtr = (char*)&data; if(0) //for testing #989
{
this->This()->read(&data,sizeof(data));
}
else
{
unsigned length = sizeof(data); unsigned length = sizeof(data);
char* dataPtr = (char*)&data;
this->This()->read(dataPtr,length); this->This()->read(dataPtr,length);
if(reverseEndianess) if(reverseEndianess)
std::reverse(dataPtr, dataPtr + length); std::reverse(dataPtr, dataPtr + length);
} }
}
template <typename T> template <typename T>
void loadSerializableBySerializeCall(T &data) void loadSerializableBySerializeCall(T &data)
@ -774,7 +780,7 @@ public:
////that const cast is evil because it allows to implicitly overwrite const objects when deserializing ////that const cast is evil because it allows to implicitly overwrite const objects when deserializing
typedef typename boost::remove_const<T>::type nonConstT; typedef typename boost::remove_const<T>::type nonConstT;
nonConstT &hlp = const_cast<nonConstT&>(data); nonConstT &hlp = const_cast<nonConstT&>(data);
hlp.serialize(*this,myVersion); hlp.serialize(*this,fileVersion);
//data.serialize(*this,myVersion); //data.serialize(*this,myVersion);
} }
@ -1018,11 +1024,13 @@ public:
std::string fName; std::string fName;
unique_ptr<std::ifstream> sfile; unique_ptr<std::ifstream> sfile;
CLoadFile(const std::string &fname, int minimalVersion = version); CLoadFile(const std::string &fname, int minimalVersion = version); //throws!
~CLoadFile(); ~CLoadFile();
int read(const void * data, unsigned size); int read(const void * data, unsigned size);
void openNextFile(const std::string &fname, int minimalVersion); void openNextFile(const std::string &fname, int minimalVersion); //throws!
void clear();
void reportState(CLogger &out); void reportState(CLogger &out);
}; };