From a5e179e530c08f4da1fc9336583252e4528eb754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Thu, 20 Jul 2023 21:38:19 +0200 Subject: [PATCH] Fix some signed integer UB in RNG and related code --- src/common/tpt-rand.cpp | 2 +- src/lua/LuaScriptInterface.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/tpt-rand.cpp b/src/common/tpt-rand.cpp index 5a076d8c7..84af99189 100644 --- a/src/common/tpt-rand.cpp +++ b/src/common/tpt-rand.cpp @@ -35,7 +35,7 @@ unsigned int RNG::operator()() int RNG::between(int lower, int upper) { unsigned int r = next(); - return static_cast(r % (upper - lower + 1)) + lower; + return static_cast(r % ((unsigned int)(upper) - (unsigned int)(lower) + 1U)) + lower; } bool RNG::chance(int nominator, unsigned int denominator) diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 5e991553c..7c4e75274 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -200,7 +200,7 @@ static int mathRandom(lua_State *l) { luaL_error(l, "interval is empty"); } - if (upper - lower + 1) + if ((unsigned int)(upper) - (unsigned int)(lower) + 1U) { lua_pushinteger(l, rng.between(lower, upper)); }