From d53daf2f26fa4fd461255859539e5ce3b7bb3e13 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Thu, 10 Jun 2021 18:28:33 +1000 Subject: [PATCH] CDImageEcm: Better error handling for corrupted dumps --- src/common/cd_image_ecm.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/common/cd_image_ecm.cpp b/src/common/cd_image_ecm.cpp index dcf9252e9..b2ef4b9d3 100644 --- a/src/common/cd_image_ecm.cpp +++ b/src/common/cd_image_ecm.cpp @@ -234,6 +234,17 @@ bool CDImageEcm::Open(const char* filename, Common::Error* error) return false; } + s64 file_size; + if (FileSystem::FSeek64(m_fp, 0, SEEK_END) != 0 || (file_size = FileSystem::FTell64(m_fp)) <= 0 || + FileSystem::FSeek64(m_fp, 0, SEEK_SET) != 0) + { + Log_ErrorPrintf("Get file size failed: errno %d", errno); + if (error) + error->SetErrno(errno); + + return false; + } + char header[4]; if (std::fread(header, sizeof(header), 1, m_fp) != 1 || header[0] != 'E' || header[1] != 'C' || header[2] != 'M' || header[3] != 0) @@ -306,6 +317,13 @@ bool CDImageEcm::Open(const char* filename, Common::Error* error) disc_offset += size; file_offset += size; count -= size; + + if (static_cast(file_offset) > file_size) + { + Log_ErrorPrintf("Out of file bounds after %zu chunks", m_data_map.size()); + if (error) + error->SetFormattedMessage("Out of file bounds after %zu chunks", m_data_map.size()); + } } } else @@ -317,6 +335,13 @@ bool CDImageEcm::Open(const char* filename, Common::Error* error) m_data_map.emplace(disc_offset, SectorEntry{file_offset, chunk_size, type}); disc_offset += chunk_size; file_offset += size; + + if (static_cast(file_offset) > file_size) + { + Log_ErrorPrintf("Out of file bounds after %zu chunks", m_data_map.size()); + if (error) + error->SetFormattedMessage("Out of file bounds after %zu chunks", m_data_map.size()); + } } }