[corlib] Add few checks for array boxing conversion of pointer types
authorMarek Safar <marek.safar@gmail.com>
Fri, 7 Apr 2017 10:15:50 +0000 (12:15 +0200)
committerMarek Safar <marek.safar@gmail.com>
Mon, 10 Apr 2017 12:46:52 +0000 (14:46 +0200)
mcs/class/corlib/System/Array.cs
mcs/class/corlib/Test/System/ArrayTest.cs

index a72d68e4738621fe272907f70d56b47ce864a686..00e0430d3de27dc3b5019a4a2cb84604d1934cc4 100644 (file)
@@ -312,12 +312,17 @@ namespace System
                public object GetValue (int index)
                {
                        if (Rank != 1)
-                               throw new ArgumentException (Locale.GetText ("Array was not a one-dimensional array."));
-                       if (index < GetLowerBound (0) || index > GetUpperBound (0))
+                               throw new ArgumentException (SR.Arg_RankMultiDimNotSupported);
+
+                       var lb  = GetLowerBound (0);
+                       if (index < lb || index > GetUpperBound (0))
                                throw new IndexOutOfRangeException (Locale.GetText (
                                        "Index has to be between upper and lower bound of the array."));
 
-                       return GetValueImpl (index - GetLowerBound (0));
+                       if (GetType ().GetElementType ().IsPointer)
+                               throw new NotSupportedException ("Type is not supported");
+
+                       return GetValueImpl (index - lb);
                }
 
                public object GetValue (int index1, int index2)
@@ -335,12 +340,17 @@ namespace System
                public void SetValue (object value, int index)
                {
                        if (Rank != 1)
-                               throw new ArgumentException (Locale.GetText ("Array was not a one-dimensional array."));
-                       if (index < GetLowerBound (0) || index > GetUpperBound (0))
+                               throw new ArgumentException (SR.Arg_RankMultiDimNotSupported);
+
+                       var lb  = GetLowerBound (0);
+                       if (index < lb || index > GetUpperBound (0))
                                throw new IndexOutOfRangeException (Locale.GetText (
                                        "Index has to be >= lower bound and <= upper bound of the array."));
 
-                       SetValueImpl (value, index - GetLowerBound (0));
+                       if (GetType ().GetElementType ().IsPointer)
+                               throw new NotSupportedException ("Type is not supported");
+
+                       SetValueImpl (value, index - lb);
                }
 
                public void SetValue (object value, int index1, int index2)
@@ -689,8 +699,9 @@ namespace System
                {
                        public Object Current {
                                get {
-                                       if (_index < 0) throw new InvalidOperationException(SR.InvalidOperation_EnumNotStarted);
-                                       if (_index >= _endIndex) throw new InvalidOperationException(SR.InvalidOperation_EnumEnded);
+                                       if (_index < 0) throw new InvalidOperationException (SR.InvalidOperation_EnumNotStarted);
+                                       if (_index >= _endIndex) throw new InvalidOperationException (SR.InvalidOperation_EnumEnded);
+                                       if (_index == 0 && _array.GetType ().GetElementType ().IsPointer) throw new NotSupportedException ("Type is not supported");
                                        return _array.GetValueImpl(_index);
                                }
                        }
index 53df7555fdbe830cacff76a44a3158f97fefb209..5279edc1a2b0dbb336d6c648be93ee00c1a2e492 100644 (file)
@@ -3687,5 +3687,17 @@ public class ArrayTest
                        Assert.AreEqual (10, ((object[])arr [i]).Length);
                }
        }
+
+       [Test]
+       public unsafe void PointerArraysBoxing ()
+       {
+               var x = new int*[10];
+               var e = x.GetEnumerator ();
+               e.MoveNext ();
+
+               Assert.Throws<NotSupportedException> (() => { var _ = e.Current; }, "#1");
+               Assert.Throws<NotSupportedException> (() => { var _ = x.GetValue (0); }, "#2");
+               Assert.Throws<NotSupportedException> (() => { x.SetValue (0, 0); }, "#3");
+       }
 }
 }