Skip to content

Commit

Permalink
Fix potential overflow and dividing by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
marksamman committed Feb 20, 2014
1 parent 5cc9e3c commit b6b7d29
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/combat.cpp
Expand Up @@ -1273,7 +1273,13 @@ void AreaCombat::copyArea(const MatrixArea* input, MatrixArea* output, MatrixOpe

MatrixArea* AreaCombat::createArea(const std::list<uint32_t>& list, uint32_t rows)
{
uint32_t cols = list.size() / rows;
uint32_t cols;
if (rows == 0) {
cols = 0;
} else {
cols = list.size() / rows;
}

MatrixArea* area = new MatrixArea(rows, cols);

uint32_t x = 0;
Expand Down
10 changes: 7 additions & 3 deletions src/creature.cpp
Expand Up @@ -1005,8 +1005,8 @@ bool Creature::setFollowCreature(Creature* creature)

double Creature::getDamageRatio(Creature* attacker) const
{
int32_t totalDamage = 0;
int32_t attackerDamage = 0;
uint32_t totalDamage = 0;
uint32_t attackerDamage = 0;

for (const auto& it : damageMap) {
const CountBlock_t& cb = it.second;
Expand All @@ -1016,6 +1016,10 @@ double Creature::getDamageRatio(Creature* attacker) const
}
}

if (totalDamage == 0) {
return 0;
}

return ((double)attackerDamage / totalDamage);
}

Expand Down Expand Up @@ -1462,7 +1466,7 @@ int64_t Creature::getEventStepTicks(bool onlyDelay) const
{
int64_t ret = getWalkDelay();
if (ret <= 0) {
int32_t stepDuration = getStepDuration();
int64_t stepDuration = getStepDuration();
if (onlyDelay && stepDuration > 0) {
ret = 1;
} else {
Expand Down

0 comments on commit b6b7d29

Please sign in to comment.