[sre] ModuleBuilder.DefineUnitializedData argument checks
authorAleksey Kliger <aleksey@xamarin.com>
Fri, 4 Aug 2017 19:17:03 +0000 (15:17 -0400)
committerAleksey Kliger <aleksey@xamarin.com>
Fri, 4 Aug 2017 20:38:24 +0000 (16:38 -0400)
1. Fix off by one in size check.
  Largest usable size is 0x3effff, first unusable size is 0x3f0000
  (Also change order of ArgumentException arguments - param name is second.
   Set it to null because of misguided CoreFX SRE test that expects a null name)
2. Check for empty string field name.

mcs/class/corlib/System.Reflection.Emit/ModuleBuilder.cs

index 57064be3027b126630d841b5bf1deae4bd0a9620..f629f8a3861ddc3c34fbde7a97b08d0dfac464a6 100644 (file)
@@ -159,21 +159,28 @@ namespace System.Reflection.Emit {
                        if (data == null)
                                throw new ArgumentNullException ("data");
 
-                       FieldBuilder fb = DefineUninitializedData (name, data.Length, 
-                                                                                                          attributes | FieldAttributes.HasFieldRVA);
+                       var maskedAttributes = attributes & ~FieldAttributes.ReservedMask;
+                       FieldBuilder fb = DefineDataImpl (name, data.Length, maskedAttributes | FieldAttributes.HasFieldRVA);
                        fb.SetRVAData (data);
 
                        return fb;
                }
 
                public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
+               {
+                       return DefineDataImpl (name, size, attributes & ~FieldAttributes.ReservedMask);
+               }
+
+               private FieldBuilder DefineDataImpl (string name, int size, FieldAttributes attributes)
                {
                        if (name == null)
                                throw new ArgumentNullException ("name");
+                       if (name == String.Empty)
+                               throw new ArgumentException ("name cannot be empty", "name");
                        if (global_type_created != null)
                                throw new InvalidOperationException ("global fields already created");
-                       if ((size <= 0) || (size > 0x3f0000))
-                               throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
+                       if ((size <= 0) || (size >= 0x3f0000))
+                               throw new ArgumentException ("Data size must be > 0 and < 0x3f0000", null as string);
 
                        CreateGlobalType ();