Skip to content

Commit

Permalink
specify 'int' type for enums which may carry unknown values (#859)
Browse files Browse the repository at this point in the history
* use "enum : int" pattern for attributes which may be extended with future states
* remove extraneous invalid-enum suppressions

Signed-off-by: Peter Hillman <peterh@wetafx.co.nz>
  • Loading branch information
peterhillman authored Jan 25, 2021
1 parent c811f98 commit 01d8d74
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 34 deletions.
13 changes: 11 additions & 2 deletions src/bin/exrenvmap/readInputImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,24 @@ readSingleImage (const char inFileName[],

Envmap type = ENVMAP_LATLONG;

if (hasEnvmap (in.header()))
type = envmap (in.header());

if (overrideType == ENVMAP_LATLONG ||
overrideType == ENVMAP_CUBE)
{
type = overrideType;
addEnvmap (header, overrideType);
}
else if (hasEnvmap (in.header()))
{
// validate type is known before using
const Envmap& typeInFile = envmap (in.header());

if (typeInFile != ENVMAP_LATLONG && typeInFile != ENVMAP_CUBE)
{
THROW (IEX::InputExc, "unknown envmap type " << int(typeInFile) );
}
type = typeInFile;
}

const Box2i &dw = in.dataWindow();
int w = dw.max.x - dw.min.x + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/exrheader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ printTimeCode (TimeCode tc)


void
printEnvmap (Envmap e)
printEnvmap (const Envmap& e)
{
switch (e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/OpenEXR/ImfDeepImageState.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER

enum DeepImageState
enum DeepImageState : int
{
DIS_MESSY = 0,
DIS_SORTED = 1,
Expand Down
12 changes: 0 additions & 12 deletions src/lib/OpenEXR/ImfDeepImageStateAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ template <>
void
DeepImageStateAttribute::writeValueTo
(OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, int version) const
#if defined (__clang__)
// _value may be an invalid value, which the clang sanitizer reports
// as undefined behavior, even though the value is acceptable in this
// context.
__attribute__((no_sanitize ("undefined")))
#endif
{
unsigned char tmp = _value;
Xdr::write <StreamIO> (os, tmp);
Expand All @@ -83,12 +77,6 @@ DeepImageStateAttribute::readValueFrom
template <>
void
DeepImageStateAttribute::copyValueFrom (const OPENEXR_IMF_INTERNAL_NAMESPACE::Attribute &other)
#if defined (__clang__)
// _value may be an invalid value, which the clang sanitizer reports
// as undefined behavior, even though the value is acceptable in this
// context.
__attribute__((no_sanitize ("undefined")))
#endif
{
_value = cast(other).value();

Expand Down
2 changes: 1 addition & 1 deletion src/lib/OpenEXR/ImfEnvmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
// Supported environment map types
//--------------------------------

enum Envmap
enum Envmap : int
{
ENVMAP_LATLONG = 0, // Latitude-longitude environment map
ENVMAP_CUBE = 1, // Cube map
Expand Down
12 changes: 0 additions & 12 deletions src/lib/OpenEXR/ImfEnvmapAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ EnvmapAttribute::staticTypeName ()
template <>
void
EnvmapAttribute::writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, int version) const
#if defined (__clang__)
// _value may be an invalid value, which the clang sanitizer reports
// as undefined behavior, even though the value is acceptable in this
// context.
__attribute__((no_sanitize ("undefined")))
#endif
{
unsigned char tmp = _value;
Xdr::write <StreamIO> (os, tmp);
Expand All @@ -81,12 +75,6 @@ EnvmapAttribute::readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int
template <>
void
EnvmapAttribute::copyValueFrom (const OPENEXR_IMF_INTERNAL_NAMESPACE::Attribute &other)
#if defined (__clang__)
// _value may be an invalid value, which the clang sanitizer reports
// as undefined behavior, even though the value is acceptable in this
// context.
__attribute__((no_sanitize ("undefined")))
#endif
{
_value = cast(other).value();

Expand Down
47 changes: 42 additions & 5 deletions src/lib/OpenEXRUtil/ImfCheckFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "ImfDeepTiledInputPart.h"
#include "ImfStdIO.h"
#include "ImfMultiPartInputFile.h"
#include "ImfStandardAttributes.h"

#include <vector>
#include <algorithm>
Expand Down Expand Up @@ -637,6 +638,37 @@ readDeepTile(T& in,bool reduceMemory , bool reduceTime)
return threw;
}

//
// EXR will read files that have out-of-range values in certain enum attributes, to allow
// values to be added in the future. This function returns 'false' if any such enum attributes
// have unknown values
//
// (implementation node: it is undefined behavior to set an enum variable to an invalid value
// this code circumvents that by casting the enums to integers and checking them that way)
//
bool enumsValid( const Header& hdr)
{
if ( hasEnvmap (hdr) )
{

const Envmap& typeInFile = envmap (hdr);
if (typeInFile != ENVMAP_LATLONG && typeInFile!= ENVMAP_CUBE)
{
return false;
}
}

if (hasDeepImageState(hdr))
{
const DeepImageState& typeInFile = deepImageState (hdr);
if (typeInFile < 0 || typeInFile >= DIS_NUMSTATES)
{
return false;
}
}

return true;
}

bool
readMultiPart(MultiPartInputFile& in,bool reduceMemory,bool reduceTime)
Expand All @@ -645,12 +677,17 @@ readMultiPart(MultiPartInputFile& in,bool reduceMemory,bool reduceTime)
for(int part = 0 ; part < in.parts() ; ++ part)
{

bool widePart = false;
Box2i b = in.header( part ).dataWindow();
if (b.max.x - b.min.x > 1000000)
{
if (!enumsValid( in.header(part)))
{
threw = true;
}

bool widePart = false;
Box2i b = in.header( part ).dataWindow();
if (b.max.x - b.min.x > 1000000)
{
widePart = true;
}
}


if (!reduceMemory || !widePart)
Expand Down

0 comments on commit 01d8d74

Please sign in to comment.