Add missing target checks to ConstructorInfo::Invoke
authorMarek Safar <marek.safar@gmail.com>
Mon, 4 Mar 2013 10:29:24 +0000 (11:29 +0100)
committerMarek Safar <marek.safar@gmail.com>
Mon, 4 Mar 2013 10:29:24 +0000 (11:29 +0100)
mcs/class/corlib/System.Reflection/MonoMethod.cs
mcs/class/corlib/System/MonoType.cs
mcs/class/corlib/Test/System.Reflection/ConstructorInfoTest.cs

index 25de7429115064560055ff949757c61a09048abd..ca70a0ad99934dc6271d3796f13566f791f14a8f 100644 (file)
@@ -496,7 +496,7 @@ namespace System.Reflection {
                }
 
                /*
-                * InternalInvoke() receives the parameters corretcly converted by the binder
+                * InternalInvoke() receives the parameters correctly converted by the binder
                 * to match the types of the method signature.
                 */
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -504,8 +504,15 @@ namespace System.Reflection {
 
                [DebuggerHidden]
                [DebuggerStepThrough]
-               public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
+               public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) 
                {
+                       if (obj == null) {
+                               if (!IsStatic)
+                                       throw new TargetException ("Instance constructor requires a target");
+                       } else if (!DeclaringType.IsInstanceOfType (obj)) {
+                               throw new TargetException ("Constructor does not match target type");                           
+                       }
+
                        return DoInvoke (obj, invokeAttr, binder, parameters, culture);
                }
 
index 985fb49732916597068ed68d88351ba3e88a53a6..fa18e2dd4d15de85ac74736db87dc427f976dfb5 100644 (file)
@@ -389,22 +389,10 @@ namespace System
                                invokeAttr |= BindingFlags.Static|BindingFlags.Instance;
 
                        if (binder == null)
-                               binder = Binder.DefaultBinder;
+                               binder = DefaultBinder;
+
                        if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
-                               /* the name is ignored */
-                               invokeAttr |= BindingFlags.DeclaredOnly;
-                               ConstructorInfo[] ctors = GetConstructors (invokeAttr);
-                               object state = null;
-                               MethodBase ctor = binder.BindToMethod (invokeAttr, ctors, ref args, modifiers, culture, namedParameters, out state);
-                               if (ctor == null) {
-                                       if (this.IsValueType && args == null)
-                                               return Activator.CreateInstanceInternal (this);
-                                       
-                                       throw new MissingMethodException ("Constructor on type '" + FullName + "' not found.");
-                               }
-                               object result = ctor.Invoke (target, invokeAttr, binder, args, culture);
-                               binder.ReorderArgumentArray (ref args, state);
-                               return result;
+                               return Activator.CreateInstance (this, invokeAttr, binder, args, culture);
                        }
                        if (name == String.Empty && Attribute.IsDefined (this, typeof (DefaultMemberAttribute))) {
                                DefaultMemberAttribute attr = (DefaultMemberAttribute) Attribute.GetCustomAttribute (this, typeof (DefaultMemberAttribute));
index 4bb532f325165e28db5fc7ee8066de2d9ef1eaa8..3b2405c10cd740b394ca2ac7fce0dd3a66ca15bd 100644 (file)
@@ -119,5 +119,19 @@ namespace MonoTests.System.Reflection
                        {
                        }
                }
+
+               [Test]
+               [ExpectedException (typeof (TargetException))]
+               public void InvokeWithNullTarget ()
+               {
+                       typeof (Foo).GetConstructors ()[0].Invoke (null, BindingFlags.Default, null, null, null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (TargetException))]
+               public void InvokeWithWrongTarget ()
+               {
+                       typeof (Foo).GetConstructors ()[0].Invoke (new object (), BindingFlags.Default, null, null, null);
+               }
        }
 }