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

[mono] Disallow casts of bounded arrays to array special interfaces #93616

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -4100,7 +4100,7 @@ mono_class_is_assignable_from_general (MonoClass *klass, MonoClass *oklass, gboo
return;
}

if (m_class_is_array_special_interface (klass) && m_class_get_rank (oklass) == 1) {
if (m_class_is_array_special_interface (klass) && m_class_get_rank (oklass) == 1 && m_class_get_byval_arg (oklass)->type == MONO_TYPE_SZARRAY) {
vargaz marked this conversation as resolved.
Show resolved Hide resolved
if (mono_class_is_gtd (klass)) {
/* klass is an array special gtd like
* IList`1<>, and oklass is X[] for some X.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

public class ReproGH93597 {
public static int Main() {
var expected = new int[] {5,4,3,2,1};

const int LowerBound = 5;

var expectedNzlba = NonZeroLowerBoundArray(expected, LowerBound);

return Helper(expectedNzlba);
return 100;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int Helper(Array a) {
IEnumerable<int> ie = null;
try {
ie = (IEnumerable<int>)a;
} catch (InvalidCastException) {
Console.WriteLine ("caught ICE, good");
return 100;
}
ie.GetEnumerator(); // mono crashes here
return 101;
}


private static Array NonZeroLowerBoundArray(Array szArrayContents, int lowerBound)
{
Array array = Array.CreateInstance(szArrayContents.GetType().GetElementType(), new int[] { szArrayContents.Length }, new int[] { lowerBound });
for (int i = 0; i < szArrayContents.Length; i++)
{
array.SetValue(szArrayContents.GetValue(i), i + lowerBound);
}
return array;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<Compile Include="GitHub_93597.cs" />
</ItemGroup>
</Project>
Loading