Skip to content

Commit 2479f3e

Browse files
committed
Fix issue with shield damage modifiers potentially under/overflowing shield damage
1 parent 31aba26 commit 2479f3e

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/New/Entity/ShieldClass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ int ShieldClass::ReceiveDamage(args_ReceiveDamage* args)
215215
const int originalShieldDamage = shieldDamage;
216216
const int min = pWHExt->Shield_ReceivedDamage_Minimum.Get(pType->ReceivedDamage_Minimum);
217217
const int max = pWHExt->Shield_ReceivedDamage_Maximum.Get(pType->ReceivedDamage_Maximum);
218-
const int minDmg = static_cast<int>(min * pWHExt->Shield_ReceivedDamage_MinMultiplier);
219-
const int maxDmg = static_cast<int>(max * pWHExt->Shield_ReceivedDamage_MaxMultiplier);
218+
const int minDmg = GeneralUtils::SafeMultiply(min, pWHExt->Shield_ReceivedDamage_MinMultiplier);
219+
const int maxDmg = GeneralUtils::SafeMultiply(max, pWHExt->Shield_ReceivedDamage_MaxMultiplier);
220220
shieldDamage = Math::clamp(shieldDamage, minDmg, maxDmg);
221221

222222
if (Phobos::DisplayDamageNumbers && shieldDamage != 0)

src/Utilities/GeneralUtils.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,27 @@ int GeneralUtils::GetColorFromColorAdd(int colorIndex)
296296

297297
return colorValue;
298298
}
299+
300+
int GeneralUtils::SafeMultiply(int value, int mult)
301+
{
302+
long long product = static_cast<long long>(value) * mult;
303+
304+
if (product > INT32_MAX)
305+
product = INT32_MAX;
306+
else if (product < INT32_MIN)
307+
product = INT32_MIN;
308+
309+
return static_cast<int>(product);
310+
}
311+
312+
int GeneralUtils::SafeMultiply(int value, double mult)
313+
{
314+
double product = static_cast<double>(value) * mult;
315+
316+
if (product > INT32_MAX)
317+
product = INT32_MAX;
318+
else if (product < INT32_MIN)
319+
product = INT32_MIN;
320+
321+
return static_cast<int>(product);
322+
}

src/Utilities/GeneralUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class GeneralUtils
3939
static CoordStruct CalculateCoordsFromDistance(CoordStruct currentCoords, CoordStruct targetCoords, int distance);
4040
static void DisplayDamageNumberString(int damage, DamageDisplayType type, CoordStruct coords, int& offset);
4141
static int GetColorFromColorAdd(int colorIndex);
42+
static int SafeMultiply(int value, int mult);
43+
static int SafeMultiply(int value, double mult);
4244
static DynamicVectorClass<ColorScheme*>* BuildPalette(const char* paletteFileName);
4345

4446
template<typename T>

0 commit comments

Comments
 (0)