From 20331bc858da8cfee968e2c5e2c027e1d5cee5e1 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 20 Apr 2021 10:12:57 +0200 Subject: [PATCH] Fix crash when using Actor GetScript or GetChild with invalid index #462 --- Source/Engine/Level/Actor.cpp | 13 ++++++++++++- Source/Engine/Level/Actor.h | 10 ++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Source/Engine/Level/Actor.cpp b/Source/Engine/Level/Actor.cpp index 8ab195ef1..ef5b2a554 100644 --- a/Source/Engine/Level/Actor.cpp +++ b/Source/Engine/Level/Actor.cpp @@ -347,6 +347,12 @@ void Actor::SetOrderInParent(int32 index) } } +Actor* Actor::GetChild(int32 index) const +{ + CHECK_RETURN(index >= 0 && index < Children.Count(), nullptr); + return Children[index]; +} + Actor* Actor::GetChild(const StringView& name) const { for (int32 i = 0; i < Children.Count(); i++) @@ -354,7 +360,6 @@ Actor* Actor::GetChild(const StringView& name) const if (Children[i]->GetName() == name) return Children[i]; } - return nullptr; } @@ -482,6 +487,12 @@ void Actor::SetName(const StringView& value) Level::callActorEvent(Level::ActorEventType::OnActorNameChanged, this, nullptr); } +Script* Actor::GetScript(int32 index) const +{ + CHECK_RETURN(index >= 0 && index < Scripts.Count(), nullptr); + return Scripts[index]; +} + Script* Actor::GetScript(const MClass* type) const { CHECK_RETURN(type, nullptr); diff --git a/Source/Engine/Level/Actor.h b/Source/Engine/Level/Actor.h index 233ebe04c..560403890 100644 --- a/Source/Engine/Level/Actor.h +++ b/Source/Engine/Level/Actor.h @@ -192,10 +192,7 @@ public: /// /// The child actor index. /// The child actor (always valid). - API_FUNCTION() FORCE_INLINE Actor* GetChild(int32 index) const - { - return Children[index]; - } + API_FUNCTION() Actor* GetChild(int32 index) const; /// /// Gets the child actor with the given name. @@ -266,10 +263,7 @@ public: /// /// The script index. /// The script (always valid). - API_FUNCTION() FORCE_INLINE Script* GetScript(int32 index) const - { - return Scripts[index]; - } + API_FUNCTION() Script* GetScript(int32 index) const; /// /// Gets the script of the given type from this actor.