Disallow conversions between Object* and Object in Array.Copy
authorJan Kotas <jkotas@microsoft.com>
Thu, 27 Apr 2017 20:48:16 +0000 (13:48 -0700)
committerJan Kotas <jkotas@microsoft.com>
Thu, 27 Apr 2017 22:39:38 +0000 (15:39 -0700)
Fixes https://github.com/dotnet/coreclr/issues/10646

Commit migrated from https://github.com/dotnet/coreclr/commit/7c57f98183491e431ac3913d5d4b70ea82886c0a

src/coreclr/src/vm/typedesc.cpp

index d05cb55..06170cb 100644 (file)
@@ -441,15 +441,17 @@ BOOL TypeDesc::CanCastTo(TypeHandle toType, TypeHandlePairList *pVisited)
     // then we must be trying to cast to a class or interface type.
     if (!toType.IsTypeDesc())
     {
-        MethodTable *pMT = GetMethodTable();
-        if (pMT == 0) {
-            // I don't have an underlying method table, therefore I'm
-            // a variable type, pointer type, function pointer type
+        if (!IsArray())
+        {
+            // I am a variable type, pointer type, function pointer type
             // etc.  I am not an object or value type.  Therefore
             // I can't be cast to an object or value type.
             return FALSE;
         }
 
+        MethodTable *pMT = GetMethodTable();
+        _ASSERTE(pMT != 0);
+
         // This does the right thing if 'type' == System.Array or System.Object, System.Clonable ...
         if (pMT->CanCastToClassOrInterface(toType.AsMethodTable(), pVisited) != 0)
         {
@@ -609,15 +611,17 @@ TypeHandle::CastResult TypeDesc::CanCastToNoGC(TypeHandle toType)
     // then we must be trying to cast to a class or interface type.
     if (!toType.IsTypeDesc())
     {
-        MethodTable *pMT = GetMethodTable();
-        if (pMT == 0) {
-            // I don't have an underlying method table, therefore I'm
-            // a variable type, pointer type, function pointer type
+        if (!IsArray())
+        {
+            // I am a variable type, pointer type, function pointer type
             // etc.  I am not an object or value type.  Therefore
             // I can't be cast to an object or value type.
             return TypeHandle::CannotCast;
         }
 
+        MethodTable *pMT = GetMethodTable();
+        _ASSERTE(pMT != 0);
+
         // This does the right thing if 'type' == System.Array or System.Object, System.Clonable ...
         return pMT->CanCastToClassOrInterfaceNoGC(toType.AsMethodTable());
     }