Fixes for use after free
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-05-15 10:08:15 +02:00
parent 39a163a1aa
commit 1639a132a9
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 10 additions and 8 deletions

View File

@ -11,9 +11,11 @@ namespace PkmnLib::Battling {
auto rate = pokemon->GetSpecies()->GetCaptureRate(); auto rate = pokemon->GetSpecies()->GetCaptureRate();
u8 bonusBall = 1; u8 bonusBall = 1;
auto* itemScript = dynamic_cast<PkmnItemUseScript*>( auto itemScript = pokemon->GetLibrary()->GetScriptResolver()->LoadItemScript(catchItem).As<PkmnItemUseScript>();
pokemon->GetLibrary()->GetScriptResolver()->LoadItemScript(catchItem).GetValue()); if (!itemScript.HasValue()) {
itemScript->ModifyPokeballCatchBonus(pokemon, &bonusBall); return {};
}
itemScript.GetValue()->ModifyPokeballCatchBonus(pokemon, &bonusBall);
u8 bonusStatus = 1; u8 bonusStatus = 1;
PKMN_HOOK(ModifyCaptureRateBonus, pokemon, pokemon, catchItem, &bonusStatus); PKMN_HOOK(ModifyCaptureRateBonus, pokemon, pokemon, catchItem, &bonusStatus);

View File

@ -83,7 +83,7 @@ void PkmnLib::Battling::Pokemon::AttemptCapture(PkmnLib::Library::Item* catchIte
Ensure(_battleData.Side.HasValue()); Ensure(_battleData.Side.HasValue());
Ensure(!IsFainted()); Ensure(!IsFainted());
Ensure(IsUsable()); Ensure(IsUsable());
Ensure(!GetBattleSide().GetValue()->IsSlotUnfillabe(this)) auto captureLibrary = auto captureLibrary =
GetLibrary().ForceAs<const BattleLibrary>()->GetCaptureLibrary(); GetLibrary().ForceAs<const BattleLibrary>()->GetCaptureLibrary();
auto result = captureLibrary->TryCatch(this, catchItem, _battleData.Battle.GetValue()->GetRandom()); auto result = captureLibrary->TryCatch(this, catchItem, _battleData.Battle.GetValue()->GetRandom());
_battleData.Battle.GetValue()->TriggerEventListener<CaptureAttemptEvent>(this, result); _battleData.Battle.GetValue()->TriggerEventListener<CaptureAttemptEvent>(this, result);

View File

@ -193,7 +193,7 @@ AngelScriptResolver::LoadScript(const ArbUt::OptionalBorrowedPtr<void>& owner, S
return new AngelScriptScript(owner, ownerType, this, t.value(), obj, _contextPool); return new AngelScriptScript(owner, ownerType, this, t.value(), obj, _contextPool);
} }
ArbUt::OptionalUniquePtr<CreatureLib::Battling::ItemUseScript> ArbUt::OptionalBorrowedPtr<CreatureLib::Battling::ItemUseScript>
AngelScriptResolver::LoadItemScript(const CreatureLib::Library::Item* item) { AngelScriptResolver::LoadItemScript(const CreatureLib::Library::Item* item) {
auto v = this->_itemUseScripts.TryGet(item); auto v = this->_itemUseScripts.TryGet(item);
if (v.has_value()) { if (v.has_value()) {

View File

@ -69,7 +69,7 @@ public:
ArbUt::OptionalUniquePtr<CreatureLib::Battling::BattleScript> ArbUt::OptionalUniquePtr<CreatureLib::Battling::BattleScript>
LoadScript(const ArbUt::OptionalBorrowedPtr<void>& owner, ScriptCategory category, LoadScript(const ArbUt::OptionalBorrowedPtr<void>& owner, ScriptCategory category,
const ArbUt::StringView& scriptName) override; const ArbUt::StringView& scriptName) override;
ArbUt::OptionalUniquePtr<CreatureLib::Battling::ItemUseScript> ArbUt::OptionalBorrowedPtr<CreatureLib::Battling::ItemUseScript>
LoadItemScript(const CreatureLib::Library::Item* item) override; LoadItemScript(const CreatureLib::Library::Item* item) override;
ArbUt::OptionalBorrowedPtr<const PkmnLib::Battling::EvolutionScript> ArbUt::OptionalBorrowedPtr<const PkmnLib::Battling::EvolutionScript>

View File

@ -45,9 +45,9 @@ static AngelScriptItemUseScript* GetScript(PkmnLib::Battling::BattleLibrary* mai
new CreatureLib::Library::SecondaryEffect(100, name, {}), nullptr, {}); new CreatureLib::Library::SecondaryEffect(100, name, {}), nullptr, {});
auto s = lib->LoadItemScript(&item); auto s = lib->LoadItemScript(&item);
auto script = dynamic_cast<AngelScriptItemUseScript*>(s.TakeOwnership()); auto script = s.As<AngelScriptItemUseScript>();
REQUIRE(script != nullptr); REQUIRE(script != nullptr);
return script; return script.GetValue();
} }
TEST_CASE("Invoke isItemUsable item use script function on empty class") { TEST_CASE("Invoke isItemUsable item use script function on empty class") {