Skip to content

Commit

Permalink
fix: prevent crash in condition light division (#3053)
Browse files Browse the repository at this point in the history
Fix #3047 

Now min light level is always 1, prevent division by zero.
  • Loading branch information
kaleohanopahala authored Nov 3, 2024
1 parent 9bed23d commit b82568a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/creatures/combat/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2540,7 +2540,7 @@ void ConditionLight::addCondition(std::shared_ptr<Creature> creature, const std:
const auto &conditionLight = condition->static_self_cast<ConditionLight>();
lightInfo.level = conditionLight->lightInfo.level;
lightInfo.color = conditionLight->lightInfo.color;
lightChangeInterval = ticks / lightInfo.level;
lightChangeInterval = ticks / std::max<uint8_t>(1, lightInfo.level);
internalLightTicks = 0;
creature->setCreatureLight(lightInfo);
g_game().changeLight(creature);
Expand All @@ -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<uint8_t>(1, static_cast<uint8_t>(value));
return true;
}

case CONDITION_PARAM_LIGHT_COLOR:
lightInfo.color = value;
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/combat/condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit b82568a

Please sign in to comment.