From b6b7d29c8e91528957d3f468cdce88a61da0e7e1 Mon Sep 17 00:00:00 2001 From: Mark Samman Date: Thu, 20 Feb 2014 13:52:53 -0500 Subject: [PATCH] Fix potential overflow and dividing by zero --- src/combat.cpp | 8 +++++++- src/creature.cpp | 10 +++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/combat.cpp b/src/combat.cpp index eda60e0c69..411a151a3f 100644 --- a/src/combat.cpp +++ b/src/combat.cpp @@ -1273,7 +1273,13 @@ void AreaCombat::copyArea(const MatrixArea* input, MatrixArea* output, MatrixOpe MatrixArea* AreaCombat::createArea(const std::list& 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; diff --git a/src/creature.cpp b/src/creature.cpp index 925ee6c80e..3e5a31e598 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -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; @@ -1016,6 +1016,10 @@ double Creature::getDamageRatio(Creature* attacker) const } } + if (totalDamage == 0) { + return 0; + } + return ((double)attackerDamage / totalDamage); } @@ -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 {