Skip to content

Commit

Permalink
fix: assigning negative value to unsigned integer (getSkillLevel) (op…
Browse files Browse the repository at this point in the history
…entibiabr#769)

Negative value was being assigned to an uint16_t, due to a wrong variable type in std::max.
  • Loading branch information
murilo09 authored Jan 25, 2023
1 parent 9de023f commit 202f089
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/creatures/players/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -761,14 +761,14 @@ class Player final : public Creature, public Cylinder
}

uint16_t getSkillLevel(uint8_t skill) const {
uint16_t skillLevel = std::max<uint16_t>(0, skills[skill].level + varSkills[skill]);
auto skillLevel = std::max<int32_t>(0, skills[skill].level + varSkills[skill]);

auto it = maxValuePerSkill.find(skill);
if (it != maxValuePerSkill.end()) {
skillLevel = std::min<uint16_t>(it->second, skillLevel);
if (auto it = maxValuePerSkill.find(skill);
it != maxValuePerSkill.end()) {
skillLevel = std::min<int32_t>(it->second, skillLevel);
}

return skillLevel;
return static_cast<uint16_t>(skillLevel);
}
uint16_t getBaseSkill(uint8_t skill) const {
return skills[skill].level;
Expand Down
4 changes: 2 additions & 2 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6599,14 +6599,14 @@ void ProtocolGame::AddPlayerSkills(NetworkMessage &msg)

msg.add<uint16_t>(player->getMagicLevel());
msg.add<uint16_t>(player->getBaseMagicLevel());
msg.add<uint16_t>(player->getBaseMagicLevel());
msg.add<uint16_t>(player->getBaseMagicLevel()); // Loyalty Bonus
msg.add<uint16_t>(player->getMagicLevelPercent() * 100);

for (uint8_t i = SKILL_FIRST; i <= SKILL_FISHING; ++i)
{
msg.add<uint16_t>(std::min<int32_t>(player->getSkillLevel(i), std::numeric_limits<uint16_t>::max()));
msg.add<uint16_t>(player->getBaseSkill(i));
msg.add<uint16_t>(player->getBaseSkill(i));
msg.add<uint16_t>(player->getBaseSkill(i)); // Loyalty Bonus
msg.add<uint16_t>(player->getSkillPercent(i) * 100);
}

Expand Down

0 comments on commit 202f089

Please sign in to comment.