-
Notifications
You must be signed in to change notification settings - Fork 616
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
Changes from 1 commit
c5d0806
80bd450
f493356
fb3b565
0cf62d6
77e7668
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
// (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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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 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; | ||
|
There was a problem hiding this comment.
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'?