Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent overflow in bytesPerDeepLineTable #1152

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/lib/OpenEXR/ImfMisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ bytesPerDeepLineTable (const Header &header,
{
const int ySampling = abs(c.channel().ySampling);
const int xSampling = abs(c.channel().xSampling);
const int pixelSize = pixelTypeSize (c.channel().type);
const uint64_t pixelSize = pixelTypeSize (c.channel().type);

// Here we transform from the domain over all pixels into the domain
// of actual samples. We want to sample points in [minY, maxY] where
Expand All @@ -148,21 +148,35 @@ bytesPerDeepLineTable (const Header &header,

for (int y = sampleMinY; y <= sampleMaxY; y+=ySampling)
{
int nBytes = 0;
uint64_t nBytes = 0;
for (int x = sampleMinX; x <= sampleMaxX; x += xSampling)
{
nBytes += pixelSize *
sampleCount(base, xStride, yStride, x, y);
static_cast<uint64_t>(sampleCount(base, xStride, yStride, x, y));
}

//
// architectures where size_t is smaller than 64 bits may overflow
// (scanlines with more than 2^32 bytes are not currently supported so this should not occur with valid files)
//
if( static_cast<uint64_t>(bytesPerLine[y - dataWindow.min.y]) + nBytes > SIZE_MAX)
{
throw IEX_NAMESPACE::IoExc("Scanline size too large");
}

bytesPerLine[y - dataWindow.min.y] += nBytes;
}
}

size_t maxBytesPerLine = 0;

for (int y = minY; y <= maxY; ++y)
{
if (maxBytesPerLine < bytesPerLine[y - dataWindow.min.y])
{
maxBytesPerLine = bytesPerLine[y - dataWindow.min.y];
}
}

return maxBytesPerLine;
}
Expand Down