From 98e8aff4c36869f102bcee64e0ff8de5a97f5062 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 21 Feb 2013 23:30:26 +0000 Subject: [PATCH] - check when loading xml's that they are not a folder - change dump world key to \ --- mk/linux/glestkeys.ini | 2 +- source/shared_lib/sources/xml/xml_parser.cpp | 31 +++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/mk/linux/glestkeys.ini b/mk/linux/glestkeys.ini index 26c607bc..3f4da82e 100644 --- a/mk/linux/glestkeys.ini +++ b/mk/linux/glestkeys.ini @@ -33,7 +33,7 @@ HotKeyCenterCameraOnSelection=G HotKeySelectIdleHarvesterUnit=I HotKeySelectBuiltBuilding=B HotKeyShowDebug=? -HotKeyDumpWorldToLog=| +HotKeyDumpWorldToLog=\ HotKeyRotateUnitDuringPlacement=R HotKeySelectDamagedUnit=D HotKeySelectStoreUnit=T diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp index 87683793..3a8e5891 100644 --- a/source/shared_lib/sources/xml/xml_parser.cpp +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -269,10 +269,15 @@ XmlNode *XmlIoRapid::load(const string &path, const std::map &map Chrono chrono; chrono.start(); if(SystemFlags::VERBOSE_MODE_ENABLED || showPerfStats) printf("Using RapidXml to load file [%s]\n",path.c_str()); - //printf("Using RapidXml to load file [%s]\n",path.c_str()); + printf("Using RapidXml to load file [%s]\n",path.c_str()); XmlNode *rootNode = NULL; try { + + if(folderExists(path) == true) { + throw megaglest_runtime_error("Can not open file: [" + path + "] as it is a folder!"); + } + #if defined(WIN32) && !defined(__MINGW32__) FILE *fp = _wfopen(utf8_decode(path).c_str(), L"rb"); ifstream xmlFile(fp); @@ -288,17 +293,29 @@ XmlNode *XmlIoRapid::load(const string &path, const std::map &map xmlFile.unsetf(ios::skipws); // Determine stream size - xmlFile.seekg(0, ios::end); - streampos size = xmlFile.tellg(); - xmlFile.seekg(0); + int64 file_size = -1; + if(xmlFile.tellg() != -1) { + streampos size1 = xmlFile.tellg(); + xmlFile.seekg(0, ios::end); + if(xmlFile.tellg() != -1) { + streampos size2 = xmlFile.tellg(); + xmlFile.seekg(0); + file_size = size2 - size1; + } + } if(showPerfStats) printf("In [%s::%s Line: %d] took msecs: " MG_I64_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); + if(file_size <= 0) { + throw megaglest_runtime_error("Invalid file size for file: [" + path + "] size = " + intToStr(file_size)); + } + //printf("File size is: " MG_I64_SPECIFIER " for [%s]\n",file_size,path.c_str()); + // Load data and add terminating 0 vector buffer; - buffer.resize((unsigned int)size + 1); - xmlFile.read(&buffer.front(), static_cast(size)); - buffer[(unsigned int)size] = 0; + buffer.resize(file_size + 1); + xmlFile.read(&buffer.front(), static_cast(file_size)); + buffer[file_size] = 0; if(showPerfStats) printf("In [%s::%s Line: %d] took msecs: " MG_I64_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis());