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

wrong string switch #2101

Closed
wwh1004 opened this issue Aug 11, 2020 · 1 comment
Closed

wrong string switch #2101

wwh1004 opened this issue Aug 11, 2020 · 1 comment
Labels
Bug Decompiler The decompiler engine itself

Comments

@wwh1004
Copy link
Contributor

wwh1004 commented Aug 11, 2020

private string DeleteFileOrDirectory(string type1, string type2, string wj)
{
	string text = Directory.GetCurrentDirectory() + wj;
	if (type1 == "QQ目录下")
	{
		text = Directory.GetCurrentDirectory() + "\\QQ\\" + ((Plugin)this).get_Client().get_QQ() + wj;
	}
	reply = $"删除失败,路径指向的文件{type2}不存在!\n详细路径:{text}";
	string text2;
	switch (text2)
	{
	default:
		if (text2.Length == 0 && File.Exists(text))
		{
			File.Delete(text);
			reply = "删除成功!";
		}
		break;
	case "":
		if (Directory.Exists(text))
		{
			Directory.Delete(text, recursive: true);
			reply = "删除成功!";
		}
		break;
	case null:
	case null:
		break;
	}
	return reply;
}

it is obviously not a string switch, even local var "text2" does not get assigned and there are duplicated cases.

i don't have source code, it is bin file and i deleted other not related code.
1.zip

ilspy version: 6.1.0.5969

@wwh1004 wwh1004 added Bug Decompiler The decompiler engine itself labels Aug 11, 2020
@dgrunwald
Copy link
Member

dgrunwald commented Aug 15, 2020

ILSpy will turn a sufficiently-long chain of if-else if string comparisons into a switch.

I see two problems here:

  • The switch transform has a bug that causes it to sometimes delete a necessary variable initialization (this leaves text2 uninitialized)
  • The switch transform needs to check for duplicate strings (here: duplicate null) and combine these (or abort the transform if these jump to different instructions -- or maybe just always abort on duplicates)

In this case if the de-duplication was implemented that would reduce the number of case labels from 3 to 2; which would disable the decompilation into a switch.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 16, 2020
ElektroKill added a commit to dnSpyEx/ILSpy that referenced this issue Aug 8, 2021
…ore is set; implement simple case-de-duplication: abort if there are any duplicate cases.
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

2 participants