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

fix undefined behavior: clamp shift in B44 Decompress #832

Merged
Merged
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion OpenEXR/IlmImf/ImfB44Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,16 @@ unpack14 (const unsigned char b[14], unsigned short s[16])

s[ 0] = (b[0] << 8) | b[1];

unsigned short shift = (b[ 2] >> 2);


//
//TODO unverified fix for undefined behavior
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TODO comment is confusing, what do you mean by 'unverified'?

// (0x20 << shift) will overflow 32 bit int if 'shift' is greater than 26
// this should only occur in corrupt files. Clamping 'shift' to 26
// prevents undefined behavior in this circumstance (but cast to unsigned short
// will still overflow)
//
unsigned short shift = std::min( 26 , b[ 2] >> 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 26, since the type is unsigned short? Why not clamp to 10 instead, since any more would overflow the 16 bits and produce the same 0 results?

Copy link
Contributor Author

@peterhillman peterhillman Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that's why I felt the need to say the fix was 'unverified' as wasn't fully sure.

IlmImfTest produces shift values of 11, so 10 is definitely not enough. It may actually need to be 31 to be sure this change won't change the decoding of broken images. shift is used to compute bias, as well as in the following lines.

My understanding is this:

All the << operators cause casts to 32 bit signed integers for those lines. That means all those lines involve 32 bit computation, which is then truncated to 16 bits. I think the implicit cast to signed arithmetic may have been unintentional.

It appears that what happens when a<<b overflows is undefined if a is signed, but defined if a is unsigned: the top bits are lost. However if b is equal to or greater than type's bit size (e.g. larger than 31 if a is 32 bit int) then the behavior is always undefined. Perhaps some 32-bit processors only take the bottom 5 bits of b, so a<<33 gives the same result as a<<1

A more conservative approach would be to clamp shift to 31, and switch all the computation to unsigned operations. This would prevent any undefined behavior. Overflows would still happen but now have well defined behavior.

unsigned short bias = (0x20 << shift);

s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
Expand Down