Skip to content

Commit 04d0950

Browse files
AMDmi3AJenbo
authored andcommitted
Fix UB in random generator
abs(INT_MIN) is undefined behavior, so process this case without calling abs()
1 parent d56ca5d commit 04d0950

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Source/engine/random.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "engine/random.hpp"
22

3+
#include <limits>
4+
35
#include "utils/stdcompat/abs.hpp"
46

57
namespace devilution {
@@ -29,7 +31,9 @@ uint32_t GetLCGEngineState()
2931

3032
int32_t GetRndSeed()
3133
{
32-
return abs(static_cast<int32_t>(sglGameSeed));
34+
const int32_t seed = static_cast<int32_t>(sglGameSeed);
35+
// since abs(INT_MIN) is undefined behavior, handle this value specially
36+
return seed == std::numeric_limits<int32_t>::min() ? std::numeric_limits<int32_t>::min() : abs(seed);
3337
}
3438

3539
int32_t AdvanceRndSeed()

0 commit comments

Comments
 (0)