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

Missing readonly on ref-local can result in ArrayTypeMismatchException #3237

Closed
dgrunwald opened this issue Jul 19, 2024 · 0 comments
Closed
Labels
Bug Decompiler The decompiler engine itself

Comments

@dgrunwald
Copy link
Member

dgrunwald commented Jul 19, 2024

Input code

using System.Collections.Generic;

public class C {
    public static void Foo(in object a, object v) {}
    public static void M1(object[] arr) {
        Foo(in arr[1], new List<int>{1,2});
    }
    public static void Main() {
        string[] arr = new string[5];
        M1(arr);
    }
}

Erroneous output

If collection initializers are disabled, or if the second argument is another complex expression that the decompiler transforms into multiple statements:

    public static void M1(object[] arr)
    {
        ref object a = ref arr[1];
        List<int> list = new List<int>();
        list.Add(1);
        list.Add(2);
        Foo(ref a, list);
    }

In the original code, in arr[1] translates to readonly.ldelema. In the decompiled code, we get a non-readonly ldelema. This results in the decompiled program failing with ArrayTypeMismatchException when the original program was successful.

A correct decompilation would use:

    public static void M1(object[] arr)
    {
        ref readonly object a = ref arr[1];
        List<int> list = new List<int>();
        list.Add(1);
        list.Add(2);
        Foo(in a, list);
    }
@dgrunwald dgrunwald added Bug Decompiler The decompiler engine itself labels Jul 19, 2024
@dgrunwald dgrunwald changed the title Missing ref readonly local support can result in ArrayTypeMismatchException Missing readonly on ref-local can result in ArrayTypeMismatchException Jul 21, 2024
mattsh247 pushed a commit to mattsh247/ILSpy that referenced this issue Jul 30, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Decompiler The decompiler engine itself
Projects
None yet
Development

No branches or pull requests

1 participant