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

Incorrect decompiled code (missing casts) #244

Open
pardeike opened this issue Sep 18, 2023 · 2 comments
Open

Incorrect decompiled code (missing casts) #244

pardeike opened this issue Sep 18, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@pardeike
Copy link

dnSpyEx version

6.4.1

Describe the Bug

If you compare System.String.Equals(string, string) between reference code, dnSpyEx and ILSpy, you will see some functional differences. While this is a specific case, other examples exist that are similar:

Reference .NET code

public static bool Equals(String a, String b)
{
    if ((Object)a == (Object)b)
        return true;

    if ((Object)a == null || (Object)b == null)
        return false;

    if (a.Length != b.Length)
        return false;

    return EqualsHelper(a, b);
}

dnSpyEx

public static bool Equals(string a, string b)
{
	return a == b || (a != null && b != null && a.Length == b.Length && string.EqualsHelper(a, b));
}

ILSpy 8.1.0.7455

public static bool Equals(string a, string b)
{
	if ((object)a == b)
	{
		return true;
	}
	if ((object)a == null || (object)b == null)
	{
		return false;
	}
	if (a.Length != b.Length)
	{
		return false;
	}
	return EqualsHelper(a, b);
}

How To Reproduce

Just decompile mscorlib .NETFramework v4.0 and compare between ILSpy and dnSpyEx and the reference code at the given url. One problem that arrise is that the dnSpy code looks like a recursion where String.Equals() calls String.== and the other way around:

public static bool operator ==(string a, string b)
{
	return string.Equals(a, b);
}
public static bool Equals(string a, string b)
{
	return a == b || (a != null && b != null && a.Length == b.Length && string.EqualsHelper(a, b));
}

which of course isn't true.

Expected Behavior

The result should make sure the == operator is not called on (string, string) as shown in the code of ILSpy.

Actual Behavior

Some casting should be shown in the decompiled code (just like in ILSpy)

Additional Context

No response

@pardeike pardeike added the bug Something isn't working label Sep 18, 2023
@funkvps
Copy link

funkvps commented Sep 19, 2023

seems also related to the version of ILSPY engine.
#5

@ElektroKill
Copy link
Member

Hi, the issue you are facing is a known limitation of the current decompiler engine. the engine is simply incapable of inserting the missing object casts there as it cannot predict the produced C# code will reference the correct member. Modern versions of ILSpy implement a sort of resolver that implements the C# member resolution specification. Using this resolver they can determine if additional casts need to be inserted or if additional qualifications on member references are needed.

Implementing such analysis logic in the current ILSpy 2.4 engine would be hard so this problem will likely be fixed when dnSpy moves to the newest ILSpy decompiler engine in the future. Progress on that is tracked by issue #5 as mentioned by @funkvps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants