Browse Source

Fixed NullReferenceException when decompiling a switch over a boolean variable that includes a default case.

pull/314/merge
Daniel Grunwald 14 years ago
parent
commit
63a55fa5b0
  1. 8
      ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs
  2. 12
      ICSharpCode.Decompiler/Tests/Switch.cs

8
ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs

@ -175,7 +175,13 @@ namespace ICSharpCode.Decompiler.Ast
}; };
} else if (node is ILSwitch) { } else if (node is ILSwitch) {
ILSwitch ilSwitch = (ILSwitch)node; ILSwitch ilSwitch = (ILSwitch)node;
if (TypeAnalysis.IsBoolean(ilSwitch.Condition.InferredType) && ilSwitch.CaseBlocks.SelectMany(cb => cb.Values).Any(val => val != 0 && val != 1)) { if (TypeAnalysis.IsBoolean(ilSwitch.Condition.InferredType) && (
from cb in ilSwitch.CaseBlocks
where cb.Values != null
from val in cb.Values
select val
).Any(val => val != 0 && val != 1))
{
// If switch cases contain values other then 0 and 1, force the condition to be non-boolean // If switch cases contain values other then 0 and 1, force the condition to be non-boolean
ilSwitch.Condition.ExpectedType = typeSystem.Int32; ilSwitch.Condition.ExpectedType = typeSystem.Int32;
} }

12
ICSharpCode.Decompiler/Tests/Switch.cs

@ -74,4 +74,16 @@ public static class Switch
return "Default"; return "Default";
} }
} }
public static string SwitchOverBool(bool b)
{
switch (b) {
case true:
return bool.TrueString;
case false:
return bool.FalseString;
default:
return null;
}
}
} }

Loading…
Cancel
Save