Skip to content


Fix UB in random generator
Browse files Browse the repository at this point in the history
abs(INT_MIN) is undefined behavior, so process this case without
calling abs()
  • Loading branch information
AMDmi3 authored and AJenbo committed May 18, 2022
1 parent d56ca5d commit 04d0950
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Source/engine/random.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "engine/random.hpp"

#include <limits>

#include "utils/stdcompat/abs.hpp"

namespace devilution {
Expand Down Expand Up @@ -29,7 +31,9 @@ uint32_t GetLCGEngineState()

int32_t GetRndSeed()
return abs(static_cast<int32_t>(sglGameSeed));
const int32_t seed = static_cast<int32_t>(sglGameSeed);
// since abs(INT_MIN) is undefined behavior, handle this value specially
return seed == std::numeric_limits<int32_t>::min() ? std::numeric_limits<int32_t>::min() : abs(seed);

int32_t AdvanceRndSeed()
Expand Down

0 comments on commit 04d0950

Please sign in to comment.