Skip to content

Commit

Permalink
Fix icsharpcode#2398: TranslateCondition: truncate condition value if…
Browse files Browse the repository at this point in the history
… necessary
  • Loading branch information
ElektroKill committed Aug 11, 2021
1 parent e2330a0 commit 4dbab34
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
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 @@ -86,6 +90,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 @@ -305,5 +305,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;
}
}
}
12 changes: 11 additions & 1 deletion ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,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 @@ -3215,7 +3220,12 @@ protected internal override TranslatedExpression VisitIfInstruction(IfInstructio
// ILAst LogicAnd/LogicOr can return a different value than 0 or 1
// if the rhs is evaluated.
// 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 (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 4dbab34

Please sign in to comment.