From 87c8b6395c5983fec12e19dfda0f4e83dead020c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Heusipp?= Date: Fri, 15 Mar 2024 11:44:11 +0000 Subject: [PATCH] Merged revision(s) 20335 from trunk/OpenMPT: [Fix] mpt/io_read/filedata_base_unseekable.hpp: Avoid memory denial-of-service when trying to read way more data than the actual file size from unseekable files. ........ git-svn-id: https://source.openmpt.org/svn/openmpt/branches/OpenMPT-1.31@20340 56274372-70c3-4bfc-bfc3-4c3a0b034d27 --- src/mpt/io_read/filedata_base_unseekable.hpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/mpt/io_read/filedata_base_unseekable.hpp b/src/mpt/io_read/filedata_base_unseekable.hpp index 874c32457b2..dbc8d4fe1e3 100644 --- a/src/mpt/io_read/filedata_base_unseekable.hpp +++ b/src/mpt/io_read/filedata_base_unseekable.hpp @@ -87,15 +87,14 @@ class FileDataUnseekable : public IFileData { return; } std::size_t alignedpos = mpt::saturate_align_up(target, QUANTUM_SIZE); - std::size_t needcount = alignedpos - cachesize; - EnsureCacheBuffer(needcount); - std::size_t readcount = InternalReadUnseekable(mpt::span(&cache[cachesize], alignedpos - cachesize)).size(); - cachesize += readcount; - if (!InternalEof()) { - // can read further - return; + while (!InternalEof() && (cachesize < alignedpos)) { + EnsureCacheBuffer(BUFFER_SIZE); + std::size_t readcount = InternalReadUnseekable(mpt::span(&cache[cachesize], BUFFER_SIZE)).size(); + cachesize += readcount; + } + if (InternalEof()) { + streamFullyCached = true; } - streamFullyCached = true; } private: