CsWinRT
CsWinRT copied to clipboard
ManagedIPropertyValueImpl.UnboxValue<T> is undefined behavior
Describe the bug
The logic used in ManagedIPropertyValueImpl to unbox arbitrary types is completely not safe and relies on undefined behavior:
https://github.com/microsoft/CsWinRT/blob/eb066e4a179767e5598fd901652a625941f0b9db/src/WinRT.Runtime/Projections/IPropertyValue.net5.cs#L199-L218
It is not legal to ever use Unsafe.As<T>(object) to alias a reference type that does not match the type conversion. In other words, Unsafe.As<T>(object) can only be used in cases where (T)obj would also succeed (it's just a performance optimization).