Browse Source

Fix #2615: Overflow check did not work reliably due to fp rounding error

pull/2626/head
Daniel Grunwald 3 years ago
parent
commit
db7d507138
  1. 3
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.cs
  2. 10
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

3
ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.cs

@ -46,6 +46,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -46,6 +46,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public const long LongMaxValue = long.MaxValue;
public const long LongMinValue = long.MinValue;
public const double Double_One_Div_LongMaxValue = 1.0842021724855044E-19;
public const double Double_One_Div_LongMaxValue_NextDouble = 1.0842021724855047E-19;
public const float FloatZero = 0f;
public const float FloatMinusZero = -0f;
public const float FloatNaN = float.NaN;

10
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -1593,8 +1593,14 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -1593,8 +1593,14 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
if (v - ai == 0)
break;
v = 1 / (v - ai);
if (Math.Abs(v) > long.MaxValue)
break; // value cannot be stored in fraction without overflow
if (Math.Abs(v) >= long.MaxValue)
{
// values greater than long.MaxValue cannot be stored in fraction without overflow.
// Because the implicit conversion of long.MaxValue to double loses precision,
// it's possible that a value v that is strictly greater than long.MaxValue will
// nevertheless compare equal, so we use ">=" to compensate.
break;
}
}
if (m[1, 0] == 0)

Loading…
Cancel
Save