From eaaf9b498f7579063442491d45cb4f4ed86d8c28 Mon Sep 17 00:00:00 2001 From: Josh Peterson Date: Fri, 26 Aug 2016 14:54:27 -0400 Subject: [PATCH] Throw an ArgumentException when a delegate is bound to an open generic method. * This change was made in Unity's old version of Mono at: https://github.com/Unity-Technologies/mono/commit/9e27edaec9a91a75d6ebf0d21654dedcab74653d --- mono/metadata/icall.c | 6 ++++++ mono/tests/Makefile.am | 1 + mono/tests/delegate-argument-exception.cs | 24 +++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 mono/tests/delegate-argument-exception.cs diff --git a/mono/metadata/icall.c b/mono/metadata/icall.c index 44ac525412d..74caa8406ab 100644 --- a/mono/metadata/icall.c +++ b/mono/metadata/icall.c @@ -6212,6 +6212,7 @@ ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, Mon MonoObject *delegate; gpointer func; MonoMethod *method = info->method; + MonoMethodSignature *sig = mono_method_signature(method); mono_class_init_checked (delegate_class, &error); if (mono_error_set_pending_exception (&error)) @@ -6236,6 +6237,11 @@ ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, Mon } } + if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) { + if (!method->is_inflated) + mono_raise_exception(mono_get_exception_argument("method", " Cannot bind to the target method because its signature differs from that of the delegate type")); + } + delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error); if (mono_error_set_pending_exception (&error)) return NULL; diff --git a/mono/tests/Makefile.am b/mono/tests/Makefile.am index b4d9d0ad866..ed8172c51ed 100644 --- a/mono/tests/Makefile.am +++ b/mono/tests/Makefile.am @@ -326,6 +326,7 @@ BASE_TEST_CS_SRC_UNIVERSAL= \ bug-323114.cs \ bug-Xamarin-5278.cs \ interlocked.cs \ + delegate-argument-exception.cs \ delegate-async-exit.cs \ delegate-delegate-exit.cs \ delegate-exit.cs \ diff --git a/mono/tests/delegate-argument-exception.cs b/mono/tests/delegate-argument-exception.cs new file mode 100644 index 00000000000..7fcef5b9433 --- /dev/null +++ b/mono/tests/delegate-argument-exception.cs @@ -0,0 +1,24 @@ +using System; + +class Test { + public static int Main () { + return new Test().CallCreateDelegate(); + } + + public int CallCreateDelegate() { + try { + var m = typeof(Test).GetMethod("Foo"); + var a = (Action) Delegate.CreateDelegate(typeof(Action), this, m); + a(); + } + catch (ArgumentException) { + return 0; + } + + return 1; + } + + public void Foo() + { + } +} \ No newline at end of file -- 2.25.1