Fix integer overflow in Random::getInt()
authorAri Suonpaa <ari.suonpaa@siru.fi>
Fri, 26 Nov 2021 09:00:58 +0000 (11:00 +0200)
committerMatthew Netsch <quic_mnetsch@quicinc.com>
Sat, 11 Dec 2021 08:28:16 +0000 (08:28 +0000)
Random integer generation was having a signed integer overflow
when calculating a range between min and max values.

When casting an overflowed value to unsigned int the result
by most (if not all) implementations would produce expected
results due to modulo wraparound. However, this is not guaranteed
to happen as signed integer overflow is regarded as undefined
behavior.

This change casts the min and max values to unsigned int which
has defined under- and overflow behavior.

VK-GL-CTS Issue: 3353
VK-GL-CTS Issue: 3355
VK-GL-CTS Issue: 3356

Affects: *

Components: Framework, Vulkan, OpenGL
Change-Id: I8dff7bf9517433e1699acfe1f6973ea8fb25e4f7

framework/delibs/decpp/deRandom.hpp

index 370c087..23c77a4 100644 (file)
@@ -96,7 +96,7 @@ inline int Random::getInt (int min, int max)
        if (min == (int)0x80000000 && max == (int)0x7fffffff)
                return (int)getUint32();
        else
-               return min + (int)(getUint32() % (deUint32)(max-min+1));
+               return min + (int)(getUint32() % ((deUint32)max - (deUint32)min + 1u));
 }
 
 // Template implementations