Browse Source

Fix #2398: TranslateCondition: truncate condition value if necessary

pull/2408/head
Daniel Grunwald 4 years ago
parent
commit
aae27900dc
  1. 10
      ICSharpCode.Decompiler.Tests/TestCases/Correctness/Comparisons.cs
  2. 10
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeAnalysisTests.cs
  3. 9
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

10
ICSharpCode.Decompiler.Tests/TestCases/Correctness/Comparisons.cs

@ -44,6 +44,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness @@ -44,6 +44,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
TestUShort(0);
TestUShort(ushort.MaxValue);
Console.WriteLine("Issue2398:");
Issue2398(0x100000000);
Console.WriteLine("OverloadedOperators:");
Console.WriteLine(IsNotNull(new OverloadedOperators()));
Console.WriteLine(IsNull(new OverloadedOperators()));
Console.WriteLine(NullIs(new OverloadedOperators()));
@ -88,6 +92,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness @@ -88,6 +92,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
Console.WriteLine("uint: {0} == Id(-1) = {1}", i, i == Id(-1));
}
static void Issue2398(long value)
{
if ((int)value != 0)
Console.WriteLine("TRUE");
}
static bool IsNull(OverloadedOperators oo)
{
return (object)oo == null;

10
ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeAnalysisTests.cs

@ -306,5 +306,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -306,5 +306,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
return (string)(object)a;
}
public static bool CompareLast32Bits(long a, long b)
{
return (int)a == (int)b;
}
public static bool Last32BitsAreZero(long a)
{
return (int)a == 0;
}
}
}

9
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -198,7 +198,12 @@ namespace ICSharpCode.Decompiler.CSharp @@ -198,7 +198,12 @@ namespace ICSharpCode.Decompiler.CSharp
public TranslatedExpression TranslateCondition(ILInstruction condition, bool negate = false)
{
Debug.Assert(condition.ResultType == StackType.I4);
var expr = Translate(condition, compilation.FindType(KnownTypeCode.Boolean));
if (expr.Type.GetStackType().GetSize() > 4)
{
expr = expr.ConvertTo(FindType(StackType.I4, expr.Type.GetSign()), this);
}
return expr.ConvertToBoolean(this, negate);
}
@ -3618,6 +3623,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -3618,6 +3623,10 @@ namespace ICSharpCode.Decompiler.CSharp
// We can only correctly translate it to C# if the rhs is of type boolean:
if (op != BinaryOperatorType.Any && (rhs.Type.IsKnownType(KnownTypeCode.Boolean) || IfInstruction.IsInConditionSlot(inst)))
{
if (rhs.Type.GetStackType().GetSize() > 4)
{
rhs = rhs.ConvertTo(FindType(StackType.I4, rhs.Type.GetSign()), this);
}
rhs = rhs.ConvertToBoolean(this);
return new BinaryOperatorExpression(condition, op, rhs)
.WithILInstruction(inst)

Loading…
Cancel
Save