Browse Source

Fix #2158: Remove PointerType support from CSharpResolver for additions/subtractions.

These have special semantics (number of elements in C# vs. number of bytes in IL), and so pointer arithmetic must go through the special HandlePointerArithmetic() code path --> the normal logic in HandleBinaryNumeric must not ever emit pointer arithmetic.
pull/2176/head
Daniel Grunwald 5 years ago
parent
commit
a26a25d689
  1. 20
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs
  2. 2
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  3. 40
      ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs

20
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs

@ -340,6 +340,26 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -340,6 +340,26 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
return p - q;
}
public unsafe long Issue2158a(void* p, void* q)
{
return (long)p - (long)q;
}
public unsafe long Issue2158b(sbyte* p, sbyte* q)
{
return (long)p - (long)q;
}
public unsafe long Issue2158c(int* p, int* q)
{
return (long)p - (long)q;
}
public unsafe long Issue2158d(SimpleStruct* p, SimpleStruct* q)
{
return (long)p - (long)q;
}
public unsafe double FixedMemberAccess(StructWithFixedSizeMembers* m, int i)
{
return (double)m->Integers[i] + m->Doubles[i];

2
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -1152,7 +1152,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1152,7 +1152,7 @@ namespace ICSharpCode.Decompiler.CSharp
/// int + ptr
/// ptr - int
/// Returns null if 'inst' is not performing pointer arithmetic.
/// This function not handle 'ptr - ptr'!
/// 'ptr - ptr' is not handled here, but in HandlePointerSubtraction()!
/// </summary>
TranslatedExpression? HandlePointerArithmetic(BinaryNumericInstruction inst, TranslatedExpression left, TranslatedExpression right)
{

40
ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs

@ -748,24 +748,6 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -748,24 +748,6 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
return BinaryOperatorResolveResult(rhsType, lhs, op, rhs);
}
if (lhsType is PointerType)
{
methodGroup = new[] {
PointerArithmeticOperator(lhsType, lhsType, KnownTypeCode.Int32),
PointerArithmeticOperator(lhsType, lhsType, KnownTypeCode.UInt32),
PointerArithmeticOperator(lhsType, lhsType, KnownTypeCode.Int64),
PointerArithmeticOperator(lhsType, lhsType, KnownTypeCode.UInt64)
};
}
else if (rhsType is PointerType)
{
methodGroup = new[] {
PointerArithmeticOperator(rhsType, KnownTypeCode.Int32, rhsType),
PointerArithmeticOperator(rhsType, KnownTypeCode.UInt32, rhsType),
PointerArithmeticOperator(rhsType, KnownTypeCode.Int64, rhsType),
PointerArithmeticOperator(rhsType, KnownTypeCode.UInt64, rhsType)
};
}
if (lhsType.Kind == TypeKind.Null && rhsType.Kind == TypeKind.Null)
return new ErrorResolveResult(SpecialType.NullType);
}
@ -813,28 +795,6 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -813,28 +795,6 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
return BinaryOperatorResolveResult(rhsType, lhs, op, rhs);
}
if (lhsType is PointerType)
{
if (rhsType is PointerType)
{
IType int64 = compilation.FindType(KnownTypeCode.Int64);
if (lhsType.Equals(rhsType))
{
return BinaryOperatorResolveResult(int64, lhs, op, rhs);
}
else
{
return new ErrorResolveResult(int64);
}
}
methodGroup = new[] {
PointerArithmeticOperator(lhsType, lhsType, KnownTypeCode.Int32),
PointerArithmeticOperator(lhsType, lhsType, KnownTypeCode.UInt32),
PointerArithmeticOperator(lhsType, lhsType, KnownTypeCode.Int64),
PointerArithmeticOperator(lhsType, lhsType, KnownTypeCode.UInt64)
};
}
if (lhsType.Kind == TypeKind.Null && rhsType.Kind == TypeKind.Null)
return new ErrorResolveResult(SpecialType.NullType);
}

Loading…
Cancel
Save