Browse Source

Fix #2202: prevent casting null to void* if the expected target-type is (U)IntPtr?

pull/2203/head
Siegfried Pammer 5 years ago
parent
commit
b9e4f7df8a
  1. 13
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstantsTests.cs
  2. 4
      ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

13
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstantsTests.cs

@ -1,9 +1,20 @@
using System.Threading.Tasks; using System;
using System.Threading.Tasks;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{ {
internal class ConstantsTests internal class ConstantsTests
{ {
public IntPtr? NullableIntPtr()
{
return null;
}
public UIntPtr? NullableUIntPtr()
{
return null;
}
public ulong Issue1308(ulong u = 8uL) public ulong Issue1308(ulong u = 8uL)
{ {
Test((u & 0xFFFFFFFFu) != 0); Test((u & 0xFFFFFFFFu) != 0);

4
ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

@ -351,7 +351,7 @@ namespace ICSharpCode.Decompiler.CSharp
.ConvertTo(targetType, expressionBuilder, checkForOverflow, allowImplicitConversion); .ConvertTo(targetType, expressionBuilder, checkForOverflow, allowImplicitConversion);
} }
} }
if (targetUType.IsKnownType(KnownTypeCode.IntPtr)) if (targetUType.IsKnownType(KnownTypeCode.IntPtr) && utype.GetStackType().IsIntegerType())
{ // Conversion to IntPtr { // Conversion to IntPtr
if (type.IsKnownType(KnownTypeCode.Int32) || type.Kind == TypeKind.NInt) if (type.IsKnownType(KnownTypeCode.Int32) || type.Kind == TypeKind.NInt)
{ {
@ -382,7 +382,7 @@ namespace ICSharpCode.Decompiler.CSharp
.ConvertTo(targetType, expressionBuilder, checkForOverflow); .ConvertTo(targetType, expressionBuilder, checkForOverflow);
} }
} }
else if (targetUType.IsKnownType(KnownTypeCode.UIntPtr)) else if (targetUType.IsKnownType(KnownTypeCode.UIntPtr) && utype.GetStackType().IsIntegerType())
{ // Conversion to UIntPtr { // Conversion to UIntPtr
if (type.IsKnownType(KnownTypeCode.UInt32) || type.Kind.IsAnyPointer() || type.Kind == TypeKind.NUInt) if (type.IsKnownType(KnownTypeCode.UInt32) || type.Kind.IsAnyPointer() || type.Kind == TypeKind.NUInt)
{ {

Loading…
Cancel
Save