Skip to content

Commit b82568a

Browse files
fix: prevent crash in condition light division (#3053)
Fix #3047 Now min light level is always 1, prevent division by zero.
1 parent 9bed23d commit b82568a

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/creatures/combat/condition.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,7 +2540,7 @@ void ConditionLight::addCondition(std::shared_ptr<Creature> creature, const std:
25402540
const auto &conditionLight = condition->static_self_cast<ConditionLight>();
25412541
lightInfo.level = conditionLight->lightInfo.level;
25422542
lightInfo.color = conditionLight->lightInfo.color;
2543-
lightChangeInterval = ticks / lightInfo.level;
2543+
lightChangeInterval = ticks / std::max<uint8_t>(1, lightInfo.level);
25442544
internalLightTicks = 0;
25452545
creature->setCreatureLight(lightInfo);
25462546
g_game().changeLight(creature);
@@ -2558,9 +2558,13 @@ bool ConditionLight::setParam(ConditionParam_t param, int32_t value) {
25582558
}
25592559

25602560
switch (param) {
2561-
case CONDITION_PARAM_LIGHT_LEVEL:
2562-
lightInfo.level = value;
2561+
case CONDITION_PARAM_LIGHT_LEVEL: {
2562+
if (value < 1) {
2563+
g_logger().warn("[ConditionLight::setParam] Trying to set invalid light value: '{}', defaulting to 1.", value);
2564+
}
2565+
lightInfo.level = std::max<uint8_t>(1, static_cast<uint8_t>(value));
25632566
return true;
2567+
}
25642568

25652569
case CONDITION_PARAM_LIGHT_COLOR:
25662570
lightInfo.color = value;

src/creatures/combat/condition.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class ConditionLight final : public Condition {
391391
bool unserializeProp(ConditionAttr_t attr, PropStream &propStream) override;
392392

393393
private:
394-
LightInfo lightInfo;
394+
LightInfo lightInfo { 1, 215 };
395395
uint32_t internalLightTicks = 0;
396396
uint32_t lightChangeInterval = 0;
397397
};

0 commit comments

Comments
 (0)