[runtime] Ensure generic type definition arity matches generic context arity
authorAleksey Kliger <aleksey@xamarin.com>
Thu, 3 Nov 2016 21:07:19 +0000 (17:07 -0400)
committerAleksey Kliger <aleksey@xamarin.com>
Fri, 4 Nov 2016 15:16:01 +0000 (11:16 -0400)
Don't instantiate generic type definitions (like Dictionary`2) with too few or too many
type arguments.

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=46250

mono/metadata/metadata.c
mono/metadata/reflection.c

index bcff8a66261a48b03da3b3165b1793036efe87c8..36a954cd8b036e73716d1d806fd2a83a465f7098 100644 (file)
@@ -2959,6 +2959,8 @@ mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst
        MonoImageSet *set;
        CollectData data;
 
+       g_assert (mono_class_get_generic_container (container_class)->type_argc == inst->type_argc);
+
        memset (&helper, 0, sizeof(helper)); // act like g_new0
        helper.container_class = container_class;
        helper.context.class_inst = inst;
index 1076479daa0b1c961f62cefbcc984cd28bc5dbee..88ecb5fddfdf84bb216c4259845dbc865569cf3e 100644 (file)
@@ -2162,6 +2162,14 @@ mono_reflection_bind_generic_parameters (MonoReflectionType *type, int type_argc
                return NULL;
        }
 
+       guint gtd_type_argc = mono_class_get_generic_container (klass)->type_argc;
+       if (gtd_type_argc != type_argc) {
+               mono_loader_unlock ();
+               mono_error_set_argument (error, "types", "The generic type definition needs %d type arguments, but was instantiated with %d ", gtd_type_argc, type_argc);
+               return NULL;
+       }
+
+
        if (klass->wastypebuilder)
                is_dynamic = TRUE;