From b82568a619b27e5835ea191e98eb1a169ccf652a Mon Sep 17 00:00:00 2001 From: "Leilani A." <168607226+kaleohanopahala@users.noreply.github.com> Date: Sun, 3 Nov 2024 15:00:47 -0300 Subject: [PATCH] fix: prevent crash in condition light division (#3053) Fix #3047 Now min light level is always 1, prevent division by zero. --- src/creatures/combat/condition.cpp | 10 +++++++--- src/creatures/combat/condition.hpp | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/creatures/combat/condition.cpp b/src/creatures/combat/condition.cpp index e49a6662991..da90b03f65a 100644 --- a/src/creatures/combat/condition.cpp +++ b/src/creatures/combat/condition.cpp @@ -2540,7 +2540,7 @@ void ConditionLight::addCondition(std::shared_ptr creature, const std: const auto &conditionLight = condition->static_self_cast(); lightInfo.level = conditionLight->lightInfo.level; lightInfo.color = conditionLight->lightInfo.color; - lightChangeInterval = ticks / lightInfo.level; + lightChangeInterval = ticks / std::max(1, lightInfo.level); internalLightTicks = 0; creature->setCreatureLight(lightInfo); g_game().changeLight(creature); @@ -2558,9 +2558,13 @@ bool ConditionLight::setParam(ConditionParam_t param, int32_t value) { } switch (param) { - case CONDITION_PARAM_LIGHT_LEVEL: - lightInfo.level = value; + case CONDITION_PARAM_LIGHT_LEVEL: { + if (value < 1) { + g_logger().warn("[ConditionLight::setParam] Trying to set invalid light value: '{}', defaulting to 1.", value); + } + lightInfo.level = std::max(1, static_cast(value)); return true; + } case CONDITION_PARAM_LIGHT_COLOR: lightInfo.color = value; diff --git a/src/creatures/combat/condition.hpp b/src/creatures/combat/condition.hpp index 1cc798624e3..50d072059a2 100644 --- a/src/creatures/combat/condition.hpp +++ b/src/creatures/combat/condition.hpp @@ -391,7 +391,7 @@ class ConditionLight final : public Condition { bool unserializeProp(ConditionAttr_t attr, PropStream &propStream) override; private: - LightInfo lightInfo; + LightInfo lightInfo { 1, 215 }; uint32_t internalLightTicks = 0; uint32_t lightChangeInterval = 0; };