Skip to content

Commit

Permalink
VSICACHE: avoid EOF read
Browse files Browse the repository at this point in the history
Do not read past EOF even if the client (e.g. VSIZIP) code say so.

Fix OSGeo#9658
  • Loading branch information
elpaso authored and olsen232 committed Apr 15, 2024
1 parent c7d7466 commit 9b58e78
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions port/cpl_vsil_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,15 @@ size_t VSICachedFile::Read(void *pBuffer, size_t nSize, size_t nCount)
/* Make sure the cache is loaded for the whole request region. */
/* ==================================================================== */
const vsi_l_offset nStartBlock = m_nOffset / m_nChunkSize;
const vsi_l_offset nEndBlock =
(m_nOffset + nRequestedBytes - 1) / m_nChunkSize;
// Calculate last block
const vsi_l_offset nLastBlock = m_nFileSize / m_nChunkSize;
vsi_l_offset nEndBlock = (m_nOffset + nRequestedBytes - 1) / m_nChunkSize;

// if nLastBlock is not 0 consider the min value to avoid out-of-range reads
if (nLastBlock != 0 && nEndBlock > nLastBlock)
{
nEndBlock = nLastBlock;
}

for (vsi_l_offset iBlock = nStartBlock; iBlock <= nEndBlock; iBlock++)
{
Expand Down

0 comments on commit 9b58e78

Please sign in to comment.