[Bug Fix] Fix possible crash in ProcessSpecialAbilities. (#2630)

* [Bug Fix] Fix possible crash in ProcessSpecialAbilities.

# Notes
- Passing an invalid special ability along such as `4,1,,-1,100` currently causes a crash since every `,` assumes it's followed by a number.
- Fixed by making sure first and second parameters are numbers and then when looping additional parameters we also check if they are numbers.

* Update mob.cpp
This commit is contained in:
Alex King 2022-12-11 09:07:06 -05:00 committed by GitHub
parent d3fac8a0cb
commit 20efa83f73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6283,37 +6283,44 @@ void Mob::ClearSpecialAbilities() {
void Mob::ProcessSpecialAbilities(const std::string &str) { void Mob::ProcessSpecialAbilities(const std::string &str) {
ClearSpecialAbilities(); ClearSpecialAbilities();
std::vector<std::string> sp = Strings::Split(str, '^'); const auto& sp = Strings::Split(str, '^');
for(auto iter = sp.begin(); iter != sp.end(); ++iter) { for (const auto& s : sp) {
std::vector<std::string> sub_sp = Strings::Split((*iter), ','); const auto& sub_sp = Strings::Split(s, ',');
if(sub_sp.size() >= 2) { if (
int ability = std::stoi(sub_sp[0]); sub_sp.size() >= 2 &&
Strings::IsNumber(sub_sp[0]) &&
Strings::IsNumber(sub_sp[1])
) {
int ability_id = std::stoi(sub_sp[0]);
int value = std::stoi(sub_sp[1]); int value = std::stoi(sub_sp[1]);
SetSpecialAbility(ability, value); SetSpecialAbility(ability_id, value);
switch(ability) {
case SPECATK_QUAD: switch (ability_id) {
if(value > 0) { case SPECATK_QUAD:
SetSpecialAbility(SPECATK_TRIPLE, 1); if (value > 0) {
} SetSpecialAbility(SPECATK_TRIPLE, 1);
break; }
case DESTRUCTIBLE_OBJECT: break;
if(value == 0) { case DESTRUCTIBLE_OBJECT:
SetDestructibleObject(false); if (value == 0) {
} else { SetDestructibleObject(false);
SetDestructibleObject(true); } else {
} SetDestructibleObject(true);
break; }
default: break;
break; default:
break;
} }
for(size_t i = 2, p = 0; i < sub_sp.size(); ++i, ++p) { for (size_t i = 2, param_id = 0; i < sub_sp.size(); ++i, ++param_id) {
if(p >= MAX_SPECIAL_ATTACK_PARAMS) { if (param_id >= MAX_SPECIAL_ATTACK_PARAMS) {
break; break;
} }
SetSpecialAbilityParam(ability, p, std::stoi(sub_sp[i])); if (Strings::IsNumber(sub_sp[i])) {
SetSpecialAbilityParam(ability_id, param_id, std::stoi(sub_sp[i]));
}
} }
} }
} }