From 86ed173d303f4e59f83e21755a05102f313cb4cb Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 24 Aug 2020 20:37:22 +0200 Subject: [PATCH] Defensive programming for Party Signed-off-by: Deukhoofd --- CInterface/Battling/CreatureParty.cpp | 4 ++-- src/Battling/Models/CreatureParty.hpp | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CInterface/Battling/CreatureParty.cpp b/CInterface/Battling/CreatureParty.cpp index cb92900..a53af41 100644 --- a/CInterface/Battling/CreatureParty.cpp +++ b/CInterface/Battling/CreatureParty.cpp @@ -16,8 +16,8 @@ export uint8_t CreatureLib_CreatureParty_GetAtIndex(Creature*& out, const Creatu export uint8_t CreatureLib_CreatureParty_Switch(CreatureParty* p, size_t a, size_t b) { Try(p->Switch(a, b);) } export uint8_t CreatureLib_CreatureParty_PackParty(CreatureParty* p) { Try(p->PackParty();) } -export Creature* CreatureLib_CreatureParty_SwapInto(CreatureParty* p, size_t index, Creature* creature) { - return p->SwapInto(index, creature); +export uint8_t CreatureLib_CreatureParty_SwapInto(Creature*& out, CreatureParty* p, size_t index, Creature* creature) { + Try(out = p->SwapInto(index, creature);) } export bool CreatureLib_CreatureParty_HasAvailableCreatures(const CreatureParty* p) { diff --git a/src/Battling/Models/CreatureParty.hpp b/src/Battling/Models/CreatureParty.hpp index 8e2aca5..1871867 100644 --- a/src/Battling/Models/CreatureParty.hpp +++ b/src/Battling/Models/CreatureParty.hpp @@ -24,6 +24,9 @@ namespace CreatureLib::Battling { void Switch(size_t a, size_t b) noexcept { _party.Swap(a, b); } Creature* SwapInto(size_t index, Creature* creature) { + if (index >= _party.Count()) { + THROW("Index was out of bounds for party.") + } auto p = _party.TakeOwnership(index); _party.Set(index, creature); return p;