diff --git a/src/pelib/ImageLoader.cpp b/src/pelib/ImageLoader.cpp index 32d1e2af2..1730bbaca 100644 --- a/src/pelib/ImageLoader.cpp +++ b/src/pelib/ImageLoader.cpp @@ -1146,29 +1146,31 @@ std::uint32_t PeLib::ImageLoader::readWriteImage( if(rva < rvaEnd) { std::uint8_t * bufferPtr = static_cast(buffer); - std::size_t pageIndex = rva / PELIB_PAGE_SIZE; - // The page index must be in range - if(pageIndex < pages.size()) + while(rva < rvaEnd) { - while(rva < rvaEnd) - { - PELIB_FILE_PAGE & page = pages[pageIndex++]; - std::uint32_t offsetInPage = rva & (PELIB_PAGE_SIZE - 1); - std::uint32_t bytesInPage = PELIB_PAGE_SIZE - offsetInPage; - - // Perhaps the last page loaded? - if(bytesInPage > (rvaEnd - rva)) - bytesInPage = (rvaEnd - rva); + std::uint32_t offsetInPage = rva & (PELIB_PAGE_SIZE - 1); + std::uint32_t bytesInPage = PELIB_PAGE_SIZE - offsetInPage; + std::size_t pageIndex = rva / PELIB_PAGE_SIZE; - // Perform the read/write operation - ReadWrite(page, bufferPtr, offsetInPage, bytesInPage); + // Perhaps the last page loaded? + if(bytesInPage > (rvaEnd - rva)) + bytesInPage = (rvaEnd - rva); - // Move pointers - bufferPtr += bytesInPage; - bytesRead += bytesInPage; - rva += bytesInPage; + // The page index must be in range + if(pageIndex < pages.size()) + { + ReadWrite(pages[pageIndex], bufferPtr, offsetInPage, bytesInPage); + } + else + { + memset(bufferPtr, 0, bytesInPage); } + + // Move pointers + bufferPtr += bytesInPage; + bytesRead += bytesInPage; + rva += bytesInPage; } }