1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-03 00:46:55 +02:00

Fix integer overflow when total resource amount is above (int max / 100)

This commit is contained in:
Ivan Savenko
2025-01-20 11:17:32 +00:00
parent 5064f6282a
commit 6b387e5d17
2 changed files with 13 additions and 8 deletions

View File

@ -67,7 +67,11 @@ void ResourceSet::positive()
void ResourceSet::applyHandicap(int percentage)
{
for(auto & elem : *this)
elem = vstd::divideAndCeil(elem * percentage, 100);
{
int64_t newAmount = vstd::divideAndCeil(static_cast<int64_t>(elem) * percentage, 100);
int64_t cap = GameConstants::PLAYER_RESOURCES_CAP;
elem = std::min(cap, newAmount);
}
}
static bool canAfford(const ResourceSet &res, const ResourceSet &price)