CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

ManagedIPropertyValueImpl.UnboxValue<T> is undefined behavior

Open Sergio0694 opened this issue 2 years ago • 0 comments

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).

Sergio0694 avatar Jan 10 '24 13:01 Sergio0694