Skip to content

Commit

Permalink
prevent invalid tile description enums
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Hillman <[email protected]>
  • Loading branch information
peterhillman authored and cary-ilm committed Aug 5, 2020
1 parent a019890 commit 4d43d43
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 36 deletions.
69 changes: 35 additions & 34 deletions OpenEXR/IlmImf/ImfHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,48 +913,49 @@ Header::sanityCheck (bool isTiled, bool isMultipartFile) const

if (isTiled)
{
if (!hasTileDescription())
{
throw IEX_NAMESPACE::ArgExc ("Tiled image has no tile "
"description attribute.");
}
if (!hasTileDescription())
{
throw IEX_NAMESPACE::ArgExc ("Tiled image has no tile "
"description attribute.");
}

const TileDescription &tileDesc = tileDescription();
const TileDescription &tileDesc = tileDescription();

if (tileDesc.xSize <= 0 || tileDesc.ySize <= 0 || tileDesc.xSize > INT_MAX || tileDesc.ySize > INT_MAX )
throw IEX_NAMESPACE::ArgExc ("Invalid tile size in image header.");
if (tileDesc.xSize <= 0 || tileDesc.ySize <= 0 || tileDesc.xSize > INT_MAX || tileDesc.ySize > INT_MAX )
throw IEX_NAMESPACE::ArgExc ("Invalid tile size in image header.");

if (maxTileWidth > 0 &&
maxTileWidth < int(tileDesc.xSize))
{
THROW (IEX_NAMESPACE::ArgExc, "The width of the tiles exceeds the maximum "
"width of " << maxTileWidth << "pixels.");
}
if (maxTileWidth > 0 &&
maxTileWidth < int(tileDesc.xSize))
{
THROW (IEX_NAMESPACE::ArgExc, "The width of the tiles exceeds the maximum "
"width of " << maxTileWidth << "pixels.");
}

if (maxTileHeight > 0 &&
maxTileHeight < int(tileDesc.ySize))
{
THROW (IEX_NAMESPACE::ArgExc, "The width of the tiles exceeds the maximum "
"width of " << maxTileHeight << "pixels.");
}
if (maxTileHeight > 0 &&
maxTileHeight < int(tileDesc.ySize))
{
THROW (IEX_NAMESPACE::ArgExc, "The width of the tiles exceeds the maximum "
"width of " << maxTileHeight << "pixels.");
}

// computes size of chunk offset table. Throws an exception if this exceeds
// the maximum allowable size
getTiledChunkOffsetTableSize(*this);
if (tileDesc.mode != ONE_LEVEL &&
tileDesc.mode != MIPMAP_LEVELS &&
tileDesc.mode != RIPMAP_LEVELS)
throw IEX_NAMESPACE::ArgExc ("Invalid level mode in image header.");

if (tileDesc.mode != ONE_LEVEL &&
tileDesc.mode != MIPMAP_LEVELS &&
tileDesc.mode != RIPMAP_LEVELS)
throw IEX_NAMESPACE::ArgExc ("Invalid level mode in image header.");
if (tileDesc.roundingMode != ROUND_UP &&
tileDesc.roundingMode != ROUND_DOWN)
throw IEX_NAMESPACE::ArgExc ("Invalid level rounding mode in image header.");

if (lineOrder != INCREASING_Y &&
lineOrder != DECREASING_Y &&
lineOrder != RANDOM_Y)
throw IEX_NAMESPACE::ArgExc ("Invalid line order in image header.");

if (tileDesc.roundingMode != ROUND_UP &&
tileDesc.roundingMode != ROUND_DOWN)
throw IEX_NAMESPACE::ArgExc ("Invalid level rounding mode in image header.");
// computes size of chunk offset table. Throws an exception if this exceeds
// the maximum allowable size
getTiledChunkOffsetTableSize(*this);

if (lineOrder != INCREASING_Y &&
lineOrder != DECREASING_Y &&
lineOrder != RANDOM_Y)
throw IEX_NAMESPACE::ArgExc ("Invalid line order in image header.");
}
else
{
Expand Down
25 changes: 23 additions & 2 deletions OpenEXR/IlmImf/ImfTileDescriptionAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,29 @@ TileDescriptionAttribute::readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream

unsigned char tmp;
Xdr::read <StreamIO> (is, tmp);
_value.mode = LevelMode (tmp & 0x0f);
_value.roundingMode = LevelRoundingMode ((tmp >> 4) & 0x0f);

//
// four bits are allocated for 'mode' for future use (16 possible values)
// but only values 0,1,2 are currently valid. '3' is a special valid enum value
// that indicates bad values have been used
//
// roundingMode can only be 0 or 1, and 2 is a special enum value for 'bad enum'
//
unsigned char levelMode = tmp & 0x0f;
if(levelMode > 3)
{
levelMode = 3;
}

_value.mode = LevelMode(levelMode);

unsigned char levelRoundingMode = (tmp >> 4) & 0x0f;
if(levelRoundingMode > 2)
{
levelRoundingMode = 2;
}

_value.roundingMode = LevelRoundingMode (levelRoundingMode);

}

Expand Down

0 comments on commit 4d43d43

Please sign in to comment.