Skip to content

Commit

Permalink
Fix IDIC casts to invalid WinRT interfaces (#1659)
Browse files Browse the repository at this point in the history
* Add catch to helper type check to catch exceptions if not winrt type rather than crashing

* Try alternative solution to do a WinRT type check

* Add test
  • Loading branch information
manodasanW authored Jul 3, 2024
1 parent c3ebc1d commit 016e9ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Tests/FunctionalTests/DynamicInterfaceCasting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,26 @@
IList<List<Point>> list2 = new List<List<Point>>();
instance2.IterableOfPointIterablesProperty = list2;

// Ensure that these don't crash but return null
if ((IWinRTObject)instance as IList<Point> != null)
{
return 105;
}

if ((IWinRTObject)instance as IList<ManagedClass> != null)
{
return 106;
}

return 100;

object SetAndGetBoxedValue(TestComponentCSharp.Class instance, object val)
{
instance.ObjectProperty = val;
return instance.ObjectProperty;
}

class ManagedClass
{
public int Number { get; set; }
}
7 changes: 7 additions & 0 deletions src/WinRT.Runtime/IWinRTObject.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ internal sealed bool IsInterfaceImplementedFallback(RuntimeTypeHandle interfaceT
}
}

// Make sure we are going to do a QI on a WinRT interface,
// otherwise we can get a helper type for a non WinRT type.
if (!Projections.IsTypeWindowsRuntimeType(type))
{
return false;
}

Type helperType = type.FindHelperType();
if (helperType is null || !helperType.IsInterface)
{
Expand Down

0 comments on commit 016e9ec

Please sign in to comment.