Skip to content

Commit

Permalink
fix: Prevent infinite loop in bit_range_convert (#3996)
Browse files Browse the repository at this point in the history
It was possible for `bit_range_convert` to be called with a 0 for the
`FROM_BITS` parameter leading to an infinite loop in its processing.

Fixes #3993: OIIO library freezes when loading R16G16 uncompressed DDS files

The test image from the above bug is now able to be read correctly.

Signed-off-by: Jesse Yurkovich <jesse.y@gmail.com>
  • Loading branch information
jessey-git authored Sep 26, 2023
1 parent 8f4a99a commit 822304b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/include/OpenImageIO/fmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ convert_type (const S &src)
/// shifted fully to the right.
template<unsigned int FROM_BITS, unsigned int TO_BITS>
inline OIIO_HOSTDEVICE unsigned int bit_range_convert(unsigned int in) {
static_assert(FROM_BITS > 0, "FROM_BITS cannot be 0");
unsigned int out = 0;
int shift = TO_BITS - FROM_BITS;
for (; shift > 0; shift -= FROM_BITS)
Expand All @@ -1244,10 +1245,12 @@ inline OIIO_HOSTDEVICE unsigned int
bit_range_convert(unsigned int in, unsigned int FROM_BITS, unsigned int TO_BITS)
{
unsigned int out = 0;
int shift = TO_BITS - FROM_BITS;
for (; shift > 0; shift -= FROM_BITS)
out |= in << shift;
out |= in >> -shift;
if (FROM_BITS) {
int shift = TO_BITS - FROM_BITS;
for (; shift > 0; shift -= FROM_BITS)
out |= in << shift;
out |= in >> -shift;
}
return out;
}

Expand Down

0 comments on commit 822304b

Please sign in to comment.