From: Ari Suonpaa Date: Fri, 26 Nov 2021 09:00:58 +0000 (+0200) Subject: Fix integer overflow in Random::getInt() X-Git-Tag: upstream/1.3.5~464 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0cef1ec1f0647f1de93ef6f4db67b8a5a1020963;p=platform%2Fupstream%2FVK-GL-CTS.git Fix integer overflow in Random::getInt() 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 --- diff --git a/framework/delibs/decpp/deRandom.hpp b/framework/delibs/decpp/deRandom.hpp index 370c087..23c77a4 100644 --- a/framework/delibs/decpp/deRandom.hpp +++ b/framework/delibs/decpp/deRandom.hpp @@ -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