From: <man...@us...> - 2014-10-25 08:27:24
|
Revision: 4485 http://sourceforge.net/p/modplug/code/4485 Author: manxorist Date: 2014-10-25 08:27:16 +0000 (Sat, 25 Oct 2014) Log Message: ----------- [Fix] Revert r4481-r4484: class FileReader does not own the file contents it corresponds to. mpt::IO::Open introduces use-after-free for the whole file contents, either by referencing an on stack std::ifstream or by referencing already-unmapped file data in case of MemoryMappedFile. Revision Links: -------------- http://sourceforge.net/p/modplug/code/4481 http://sourceforge.net/p/modplug/code/4484 Modified Paths: -------------- trunk/OpenMPT/common/mptIO.cpp trunk/OpenMPT/common/mptIO.h trunk/OpenMPT/soundlib/Load_itp.cpp trunk/OpenMPT/soundlib/Load_mt2.cpp Modified: trunk/OpenMPT/common/mptIO.cpp =================================================================== --- trunk/OpenMPT/common/mptIO.cpp 2014-10-25 02:17:50 UTC (rev 4484) +++ trunk/OpenMPT/common/mptIO.cpp 2014-10-25 08:27:16 UTC (rev 4485) @@ -22,14 +22,6 @@ #include <stdio.h> -#ifdef MPT_WITH_PATHSTRING -#include "../soundlib/FileReader.h" -#ifdef MPT_FILEREADER_STD_ISTREAM -#include "mptFstream.h" -#else -#include "../mptrack/MemoryMappedFile.h" -#endif // MPT_FILEREADER_STD_ISTREAM -#endif // MPT_WITH_PATHSTRING OPENMPT_NAMESPACE_BEGIN @@ -38,31 +30,8 @@ namespace IO { -#ifdef MPT_WITH_PATHSTRING -FileReader Open(const mpt::PathString &filename) -{ -#ifdef MPT_FILEREADER_STD_ISTREAM - mpt::ifstream f(filename, std::ios_base::binary); - if(f.good()) -#ifdef MODPLUG_TRACKER - return FileReader(&f, &filename); -#else - return FileReader(&f); -#endif // MODPLUG_TRACKER - else - return FileReader(); -#else - CMappedFile f; - if(f.Open(filename)) - return f.GetFile(); - else - return FileReader(); -#endif // MPT_FILEREADER_STD_ISTREAM -} -#endif // MPT_WITH_PATHSTRING - //STATIC_ASSERT(sizeof(std::streamoff) == 8); // Assert 64bit file support. bool IsValid(std::ostream & f) { return !f.fail(); } bool IsValid(std::istream & f) { return !f.fail(); } Modified: trunk/OpenMPT/common/mptIO.h =================================================================== --- trunk/OpenMPT/common/mptIO.h 2014-10-25 02:17:50 UTC (rev 4484) +++ trunk/OpenMPT/common/mptIO.h 2014-10-25 08:27:16 UTC (rev 4485) @@ -31,12 +31,9 @@ OPENMPT_NAMESPACE_BEGIN -class FileReader; namespace mpt { -class PathString; - namespace IO { typedef int64 Offset; @@ -50,11 +47,8 @@ return (static_cast<IO::Offset>(mpt::saturate_cast<Toff>(off)) == off); } -#ifdef MPT_WITH_PATHSTRING -// Open a file (either stream or memory-mapped, depending on program configuration) and immediately return FileReader for the file -FileReader Open(const mpt::PathString &filename); -#endif // MPT_WITH_PATHSTRING + bool IsValid(std::ostream & f); bool IsValid(std::istream & f); bool IsValid(std::iostream & f); @@ -200,7 +194,7 @@ { byte = 0; if(!IO::ReadIntLE<uint8>(f, byte)) result = false; - v |= (static_cast<uint16>(byte) << (((i+1)*8) - 1)); + v |= (static_cast<uint16>(byte) << (((i+1)*8) - 1)); } return result; } @@ -220,7 +214,7 @@ { byte = 0; if(!IO::ReadIntLE<uint8>(f, byte)) result = false; - v |= (static_cast<uint32>(byte) << (((i+1)*8) - 2)); + v |= (static_cast<uint32>(byte) << (((i+1)*8) - 2)); } return result; } @@ -240,7 +234,7 @@ { byte = 0; if(!IO::ReadIntLE<uint8>(f, byte)) result = false; - v |= (static_cast<uint64>(byte) << (((i+1)*8) - 2)); + v |= (static_cast<uint64>(byte) << (((i+1)*8) - 2)); } return result; } @@ -268,7 +262,7 @@ } str.push_back(c); } - return true; + return true; } @@ -555,7 +549,7 @@ }; -#endif +#endif class FileDataContainerMemory @@ -637,4 +631,4 @@ -OPENMPT_NAMESPACE_END \ No newline at end of file +OPENMPT_NAMESPACE_END Modified: trunk/OpenMPT/soundlib/Load_itp.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_itp.cpp 2014-10-25 02:17:50 UTC (rev 4484) +++ trunk/OpenMPT/soundlib/Load_itp.cpp 2014-10-25 08:27:16 UTC (rev 4485) @@ -20,6 +20,9 @@ #include "ITTools.h" #ifdef MODPLUG_TRACKER #include "../mptrack/TrackerSettings.h" +#if !defined(MPT_FILEREADER_STD_ISTREAM) +#include "../mptrack/MemoryMappedFile.h" +#endif #ifndef MODPLUG_NO_FILESAVE #include "../common/mptFstream.h" #endif @@ -260,7 +263,18 @@ { if(m_szInstrumentPath[ins].empty()) continue; - FileReader file(mpt::IO::Open(m_szInstrumentPath[ins])); +#if defined(MPT_FILEREADER_STD_ISTREAM) + mpt::ifstream f(m_szInstrumentPath[ins], std::ios_base::binary); + if(!f.good()) + continue; + FileReader file(&f, &m_szInstrumentPath[ins]); +#else + CMappedFile f; + if(!f.Open(m_szInstrumentPath[ins])) + continue; + FileReader file = f.GetFile(); +#endif + if(file.IsValid()) ReadInstrumentFromFile(ins + 1, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad); } Modified: trunk/OpenMPT/soundlib/Load_mt2.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_mt2.cpp 2014-10-25 02:17:50 UTC (rev 4484) +++ trunk/OpenMPT/soundlib/Load_mt2.cpp 2014-10-25 08:27:16 UTC (rev 4485) @@ -12,6 +12,12 @@ #include "stdafx.h" #include "Loaders.h" #ifdef MODPLUG_TRACKER +// For loading external samples +#if defined(MPT_FILEREADER_STD_ISTREAM) +#include "../common/mptFstream.h" +#else +#include "../mptrack/MemoryMappedFile.h" +#endif #include "../mptrack/Moddoc.h" #endif @@ -1075,7 +1081,16 @@ path = path.RelativePathToAbsolute(mt2FileName.GetPath()); } - FileReader sampleFile(mpt::IO::Open(path)); +#if defined(MPT_FILEREADER_STD_ISTREAM) + mpt::ifstream f(path, std::ios_base::binary); + if(f.good()) + sampleFile = FileReader(&f, &path); +#else + CMappedFile f; + FileReader sampleFile; + if(f.Open(path)) + sampleFile = f.GetFile(); +#endif if(sampleFile.IsValid()) ReadSampleFromFile(i + 1, sampleFile, false); #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |