From 110903788440e4d9eaa4eb10b25714b70ac9d661 Mon Sep 17 00:00:00 2001 From: peterhillman Date: Fri, 20 Nov 2020 08:30:08 +1300 Subject: [PATCH] sanity check ScanlineInput bytesPerLine instead of lineOffset size (#863) Signed-off-by: Peter Hillman Co-authored-by: Cary Phillips Signed-off-by: Cary Phillips --- OpenEXR/IlmImf/ImfScanLineInputFile.cpp | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/OpenEXR/IlmImf/ImfScanLineInputFile.cpp b/OpenEXR/IlmImf/ImfScanLineInputFile.cpp index 2a559654b2..34d4f19ab7 100644 --- a/OpenEXR/IlmImf/ImfScanLineInputFile.cpp +++ b/OpenEXR/IlmImf/ImfScanLineInputFile.cpp @@ -1108,6 +1108,33 @@ void ScanLineInputFile::initialize(const Header& header) _data->minY = dataWindow.min.y; _data->maxY = dataWindow.max.y; + Compression comp = _data->header.compression(); + + _data->linesInBuffer = + numLinesInBuffer (comp); + + int lineOffsetSize = (dataWindow.max.y - dataWindow.min.y + + _data->linesInBuffer) / _data->linesInBuffer; + + // + // avoid allocating excessive memory due to large lineOffsets and bytesPerLine table sizes. + // If the chunktablesize claims to be large, + // check the file is big enough to contain the lineOffsets table before allocating memory + // in the bytesPerLineTable and the lineOffsets table. + // Attempt to read the last entry in the table. Either the seekg() or the read() + // call will throw an exception if the file is too small to contain the table + // + if (lineOffsetSize * _data->linesInBuffer > gLargeChunkTableSize) + { + Int64 pos = _streamData->is->tellg(); + _streamData->is->seekg(pos + (lineOffsetSize-1)*sizeof(Int64)); + Int64 temp; + OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read (*_streamData->is, temp); + _streamData->is->seekg(pos); + + } + + size_t maxBytesPerLine = bytesPerLineTable (_data->header, _data->bytesPerLine);