Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[runtime] Disallow casts between int[] and IList<int?> etc. #6486

Merged
merged 1 commit into from Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions mono/metadata/class.c
Expand Up @@ -8411,13 +8411,15 @@ mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)

// If the target we're trying to cast to is a valuetype, we must account of weird valuetype equivalences such as IntEnum <> int or uint <> int
// We can't apply it for ref types as this would go wrong with arrays - IList<byte[]> would have byte tested
if (iface_klass->valuetype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that comment above should be moved into the if block imho

iface_klass = iface_klass->cast_class;
if (!mono_class_is_nullable (iface_klass)) {
if (iface_klass->valuetype)
iface_klass = iface_klass->cast_class;

//array covariant casts only operates on scalar to scalar
//This is so int[] can't be casted to IComparable<int>[]
if (!(obj_klass->valuetype && !iface_klass->valuetype) && mono_class_is_assignable_from (iface_klass, obj_klass))
return TRUE;
//array covariant casts only operates on scalar to scalar
//This is so int[] can't be casted to IComparable<int>[]
if (!(obj_klass->valuetype && !iface_klass->valuetype) && mono_class_is_assignable_from (iface_klass, obj_klass))
return TRUE;
}
}

if (mono_class_has_variant_generic_params (klass)) {
Expand Down
6 changes: 6 additions & 0 deletions mono/mini/objects.cs
@@ -1,6 +1,7 @@
using System;
using System.Text;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -787,6 +788,11 @@ class Duper: Super {
}
if (!ok)
return 12;

object arr = new int [10];
if (arr is IList<int?>)
return 13;

return 0;
}

Expand Down