Skip to content

Commit

Permalink
Fix #2398: TranslateCondition: truncate condition value if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed May 7, 2021
1 parent 8d70d63 commit aae2790
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Correctness/Comparisons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static int Main()
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()));
Expand Down Expand Up @@ -88,6 +92,12 @@ static void TestUInt(uint i)
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;
Expand Down
10 changes: 10 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeAnalysisTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,15 @@ public static string ImpossibleCast2(Action a)
{
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 changes: 9 additions & 0 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ public TranslatedExpression Translate(ILInstruction inst, IType typeHint = null)

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);
}

Expand Down Expand Up @@ -3618,6 +3623,10 @@ protected internal override TranslatedExpression VisitIfInstruction(IfInstructio
// 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)
Expand Down

0 comments on commit aae2790

Please sign in to comment.