Skip to content

Commit bd80227

Browse files
jessey-gitlgritz
authored andcommitted
fix: Prevent infinite loop in bit_range_convert (AcademySoftwareFoundation#3996)
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 AcademySoftwareFoundation#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 <[email protected]>
1 parent 101b8bb commit bd80227

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/include/OpenImageIO/fmath.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,7 @@ convert_type (const S &src)
12291229
/// shifted fully to the right.
12301230
template<unsigned int FROM_BITS, unsigned int TO_BITS>
12311231
inline OIIO_HOSTDEVICE unsigned int bit_range_convert(unsigned int in) {
1232+
static_assert(FROM_BITS > 0, "FROM_BITS cannot be 0");
12321233
unsigned int out = 0;
12331234
int shift = TO_BITS - FROM_BITS;
12341235
for (; shift > 0; shift -= FROM_BITS)
@@ -1244,10 +1245,12 @@ inline OIIO_HOSTDEVICE unsigned int
12441245
bit_range_convert(unsigned int in, unsigned int FROM_BITS, unsigned int TO_BITS)
12451246
{
12461247
unsigned int out = 0;
1247-
int shift = TO_BITS - FROM_BITS;
1248-
for (; shift > 0; shift -= FROM_BITS)
1249-
out |= in << shift;
1250-
out |= in >> -shift;
1248+
if (FROM_BITS) {
1249+
int shift = TO_BITS - FROM_BITS;
1250+
for (; shift > 0; shift -= FROM_BITS)
1251+
out |= in << shift;
1252+
out |= in >> -shift;
1253+
}
12511254
return out;
12521255
}
12531256

0 commit comments

Comments
 (0)