Skip to content

Commit b8260b4

Browse files
修正:UnLua函数绑定多个不同签名的代理导致崩溃 Tencent#660
1 parent 481be81 commit b8260b4

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Plugins/UnLua/Source/UnLua/Private/LuaDelegateHandler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ void ULuaDelegateHandler::BindTo(FScriptDelegate* InDelegate)
3636

3737
void ULuaDelegateHandler::AddTo(FMulticastDelegateProperty* InProperty, void* InDelegate)
3838
{
39+
check(InDelegate);
40+
3941
Delegate = InDelegate;
4042
FScriptDelegate DynamicDelegate;
4143
DynamicDelegate.BindUFunction(this, NAME_Dummy);

Plugins/UnLua/Source/UnLua/Private/Registries/DelegateRegistry.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,31 @@ namespace UnLua
104104
Handler->Reset();
105105
}
106106

107+
void FDelegateRegistry::CheckSignatureCompatible(lua_State* L, ULuaDelegateHandler* Handler, void* OtherDelegate)
108+
{
109+
check(L);
110+
check(Handler);
111+
112+
if (!CheckSignatureCompatible(Handler->Delegate, OtherDelegate))
113+
luaL_error(L, "delegate handler signatures are not compatible");
114+
}
115+
116+
bool FDelegateRegistry::CheckSignatureCompatible(void* ADelegate, void* BDelegate)
117+
{
118+
if (ADelegate == BDelegate)
119+
return true;
120+
121+
auto AInfo = Delegates.Find(ADelegate);
122+
if (!AInfo || !AInfo->SignatureFunction)
123+
return false;
124+
125+
auto BInfo = Delegates.Find(BDelegate);
126+
if (!BInfo|| !BInfo->SignatureFunction)
127+
return false;
128+
129+
return AInfo->SignatureFunction->IsSignatureCompatibleWith(BInfo->SignatureFunction);
130+
}
131+
107132
#pragma region FScriptDelgate
108133

109134
void FDelegateRegistry::Register(void* Delegate, FProperty* Property, UObject* Owner)
@@ -221,6 +246,7 @@ namespace UnLua
221246
const auto Cached = CachedHandlers.Find(DelegatePair);
222247
if (Cached && Cached->IsValid())
223248
{
249+
CheckSignatureCompatible(L, Cached->Get(), Delegate);
224250
(*Cached)->AddTo(Info.MulticastProperty, Delegate);
225251
Info.Handlers.Add(*Cached);
226252
return;

Plugins/UnLua/Source/UnLua/Private/Registries/DelegateRegistry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ namespace UnLua
8282
void NotifyHandlerBeginDestroy(ULuaDelegateHandler* Handler);
8383

8484
private:
85+
void CheckSignatureCompatible(lua_State* L, ULuaDelegateHandler* Handler, void* OtherDelegate);
86+
87+
bool CheckSignatureCompatible(void* ADelegate, void* BDelegate);
88+
8589
TSharedPtr<FFunctionDesc> GetSignatureDesc(const void* Delegate);
8690

8791
ULuaDelegateHandler* CreateHandler(int LuaRef, UObject* Owner, UObject* SelfObject);

0 commit comments

Comments
 (0)