* MethodBuilder.cs: In CreateMethodBody, throw ArgumentOutOfRangeException
authorGert Driesen <drieseng@users.sourceforge.net>
Wed, 22 Jun 2005 18:04:04 +0000 (18:04 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Wed, 22 Jun 2005 18:04:04 +0000 (18:04 -0000)
instead of ArgumentException when count is not within range of array. Do not
allow zero-length method body to be emitted when using 2.0 profile. Fixes
bug #75236.
* MethodBuilderTest.cs: Updated existing tests to no longer define zero-length
method body. Added tests for bug #75236.

svn path=/trunk/mcs/; revision=46376

mcs/class/corlib/System.Reflection.Emit/ChangeLog
mcs/class/corlib/System.Reflection.Emit/MethodBuilder.cs
mcs/class/corlib/Test/System.Reflection.Emit/ChangeLog
mcs/class/corlib/Test/System.Reflection.Emit/MethodBuilderTest.cs

index b9fae1a4538c70d11916db9e2cad54acbffb6d0e..eb8c0e5f9c860049b3b0e681d9c0dd55a60bc638 100644 (file)
@@ -3,6 +3,13 @@
        * AssemblyBuilder.cs: Override UnprotectedGetName to set the public 
        key (if available).
 
+2005-06-12  Gert Driesen <drieseng@users.sourceforge.net>
+
+       * MethodBuilder.cs: In CreateMethodBody, throw 
+       ArgumentOutOfRangeException instead of ArgumentException when count 
+       is not within range of array. Do not allow zero length method body 
+       to be emitted when using 2.0 profile. Fixes bug #75236.
+
 2005-06-12  Gert Driesen <drieseng@users.sourceforge.net>
 
        * FieldBuilder.cs: FieldBuilder.FieldHandle should throw
index c4574bbcf79e326611985c0553ec36096f9b1d5b..125fd2d886ed797b3a4e088b3a194ed839a33ae4 100644 (file)
@@ -215,7 +215,7 @@ namespace System.Reflection.Emit {
 
                public void CreateMethodBody( byte[] il, int count) {
                        if ((il != null) && ((count < 0) || (count > il.Length)))
-                               throw new ArgumentException ("Index was out of range.  Must be non-negative and less than the size of the collection.");
+                               throw new ArgumentOutOfRangeException ("Index was out of range.  Must be non-negative and less than the size of the collection.");
 
                        if ((code != null) || type.is_created)
                                throw new InvalidOperationException ("Type definition of the method is complete.");
@@ -275,7 +275,12 @@ namespace System.Reflection.Emit {
 
                internal void fixup () {
                        if (((attrs & (MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)) == 0) && ((iattrs & (MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall)) == 0)) {
+#if NET_2_0
+                               // do not allow zero length method body on MS.NET 2.0 (and higher)
+                               if (((ilgen == null) || (ILGenerator.Mono_GetCurrentOffset (ilgen) == 0)) && (code == null || code.Length == 0))
+#else
                                if (((ilgen == null) || (ILGenerator.Mono_GetCurrentOffset (ilgen) == 0)) && (code == null))
+#endif
                                        throw new InvalidOperationException ("Method '" + Name + "' does not have a method body.");
                        }
                        if (ilgen != null)
index 68a4b35795f12b7580713925eb33840c8fa4ee14..ee9350b6f8260d821ab47f1d3f774696365a9788 100644 (file)
@@ -1,3 +1,8 @@
+2005-06-22  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * MethodBuilderTest.cs: Updated existing tests to no longer define
+       zero-length method body. Added tests for bug #75236.
+
 2005-06-14  Sebastien Pouliot  <sebastien@ximian.com>
 
        * AssemblyBuilderTest.cs: Split AssemblyName_PublicKey to move culture
index 8db0b3a51d04a69a0ef3633ddd55b1c4be323767..7b4e42d6890988ab14611d6d92e31119490e8d6b 100644 (file)
@@ -123,7 +123,7 @@ public class MethodBuilderTest : Assertion
        public void TestMethodHandleComplete () {
                MethodBuilder mb = genClass.DefineMethod (
                        genMethodName (), 0, typeof (void), new Type [0]);
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                genClass.CreateType ();
 
                RuntimeMethodHandle handle = mb.MethodHandle;
@@ -192,14 +192,14 @@ public class MethodBuilderTest : Assertion
                try {
                        mb.CreateMethodBody (new byte[1], -1);
                        Fail ();
-               } catch (ArgumentException) {
+               } catch (ArgumentOutOfRangeException) {
                }
 
                // Check arguments 2.
                try {
                        mb.CreateMethodBody (new byte[1], 2);
                        Fail ();
-               } catch (ArgumentException) {
+               } catch (ArgumentOutOfRangeException) {
                }
 
                mb.CreateMethodBody (new byte[2], 1);
@@ -236,7 +236,7 @@ public class MethodBuilderTest : Assertion
                        new Type[2] {
                        typeof(int), typeof(int)
                });
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                tb.CreateType ();
                mb.DefineParameter (-5, ParameterAttributes.None, "param1");
        }
@@ -251,7 +251,7 @@ public class MethodBuilderTest : Assertion
                        new Type[2] {
                        typeof(int), typeof(int)
                });
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                tb.CreateType ();
                mb.DefineParameter (1, ParameterAttributes.None, "param1");
        }
@@ -285,8 +285,7 @@ public class MethodBuilderTest : Assertion
                mb.DefineParameter (1, 0, "param1");
                mb.DefineParameter (2, 0, null);
 
-               // Can not be called on a created type
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                tb.CreateType ();
                try {
                        mb.DefineParameter (1, 0, "param1");
@@ -296,6 +295,31 @@ public class MethodBuilderTest : Assertion
                }
        }
 
+       [Test]
+#if NET_2_0
+       // MS.NET 2.x no longer allows a zero length method body
+       // to be emitted
+       [ExpectedException (typeof (InvalidOperationException))]
+#endif
+       public void ZeroLengthBodyTest1 ()
+       {
+               MethodBuilder mb = genClass.DefineMethod (
+                       genMethodName (), 0, typeof (void), 
+                       new Type [2] { typeof(int), typeof(int) });
+               mb.CreateMethodBody (new byte[2], 0);
+               genClass.CreateType ();
+       }
+
+       // A zero length method body can be created
+       [Test]
+       public void ZeroLengthBodyTest2 ()
+       {
+               MethodBuilder mb = genClass.DefineMethod (
+                       genMethodName (), 0, typeof (void), 
+                       new Type [2] { typeof(int), typeof(int) });
+               mb.CreateMethodBody (new byte[2], 0);
+       }
+
        [Test]
        public void TestHashCode ()
        {
@@ -364,8 +388,7 @@ public class MethodBuilderTest : Assertion
                                          MethodImplAttributes.OPTIL, 
                                          mb.GetMethodImplementationFlags ());
 
-               // Can not be called on a created type
-               mb.CreateMethodBody (new byte[2], 0);
+               mb.CreateMethodBody (new byte[2], 1);
                mb.SetImplementationFlags (MethodImplAttributes.Managed);
                tb.CreateType ();
                try {