COMMON: Fix undefined behaviour in RandomSource constructor

The 'time.tm_year * 86400 * 366' line caused the result to overflow
what can be stored in an int; and signed int overflow is undefined
behaviour. The result goes into an unsigned int anyway, so now
all the intermediate computations are also done with unsigned int.
It still overflows (not on this line, but on the next one), but
that is fine as the standard guarantees that unsigned int overflow
wraps around.
This commit is contained in:
Thierry Crozat 2022-10-31 21:12:52 +00:00
parent 088d701f24
commit eff98b0c56

View File

@ -36,10 +36,10 @@ RandomSource::RandomSource(const String &name) {
#else
TimeDate time;
g_system->getTimeAndDate(time);
uint32 newSeed = time.tm_sec + time.tm_min * 60 + time.tm_hour * 3600;
newSeed += time.tm_mday * 86400 + time.tm_mon * 86400 * 31;
newSeed += time.tm_year * 86400 * 366;
newSeed = newSeed * 1000 + g_system->getMillis();
uint32 newSeed = time.tm_sec + time.tm_min * 60U + time.tm_hour * 3600U;
newSeed += time.tm_mday * 86400U + time.tm_mon * 86400U * 31U;
newSeed += time.tm_year * 86400U * 366U;
newSeed = newSeed * 1000U + g_system->getMillis();
setSeed(newSeed);
#endif
}