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();
u8 bonusBall = 1;
auto* itemScript = dynamic_cast<PkmnItemUseScript*>(
pokemon->GetLibrary()->GetScriptResolver()->LoadItemScript(catchItem).GetValue());
itemScript->ModifyPokeballCatchBonus(pokemon, &bonusBall);
auto itemScript = pokemon->GetLibrary()->GetScriptResolver()->LoadItemScript(catchItem).As<PkmnItemUseScript>();
if (!itemScript.HasValue()) {
return {};
}
itemScript.GetValue()->ModifyPokeballCatchBonus(pokemon, &bonusBall);
u8 bonusStatus = 1;
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(!IsFainted());
Ensure(IsUsable());
Ensure(!GetBattleSide().GetValue()->IsSlotUnfillabe(this)) auto captureLibrary =
auto captureLibrary =
GetLibrary().ForceAs<const BattleLibrary>()->GetCaptureLibrary();
auto result = captureLibrary->TryCatch(this, catchItem, _battleData.Battle.GetValue()->GetRandom());
_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);
}
ArbUt::OptionalUniquePtr<CreatureLib::Battling::ItemUseScript>
ArbUt::OptionalBorrowedPtr<CreatureLib::Battling::ItemUseScript>
AngelScriptResolver::LoadItemScript(const CreatureLib::Library::Item* item) {
auto v = this->_itemUseScripts.TryGet(item);
if (v.has_value()) {

View File

@ -69,7 +69,7 @@ public:
ArbUt::OptionalUniquePtr<CreatureLib::Battling::BattleScript>
LoadScript(const ArbUt::OptionalBorrowedPtr<void>& owner, ScriptCategory category,
const ArbUt::StringView& scriptName) override;
ArbUt::OptionalUniquePtr<CreatureLib::Battling::ItemUseScript>
ArbUt::OptionalBorrowedPtr<CreatureLib::Battling::ItemUseScript>
LoadItemScript(const CreatureLib::Library::Item* item) override;
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, {});
auto s = lib->LoadItemScript(&item);
auto script = dynamic_cast<AngelScriptItemUseScript*>(s.TakeOwnership());
auto script = s.As<AngelScriptItemUseScript>();
REQUIRE(script != nullptr);
return script;
return script.GetValue();
}
TEST_CASE("Invoke isItemUsable item use script function on empty class") {