From f6cd2b40d35ca362c5cbb6b02414e286d1b27121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Mon, 21 Jun 2021 09:29:25 +0100 Subject: [PATCH] Fix unbox.any for GUID type - Add GUID to WellKnownTypes. - Add code to PerformUnboxing to check for valid GUID unboxing. --- src/CLR/Core/CLR_RT_HeapBlock.cpp | 15 +++++++++++++++ src/CLR/Core/TypeSystem.cpp | 2 ++ src/CLR/Include/nanoCLR_Runtime.h | 2 ++ 3 files changed, 19 insertions(+) diff --git a/src/CLR/Core/CLR_RT_HeapBlock.cpp b/src/CLR/Core/CLR_RT_HeapBlock.cpp index bad1178b2c..f4cbfe4ade 100644 --- a/src/CLR/Core/CLR_RT_HeapBlock.cpp +++ b/src/CLR/Core/CLR_RT_HeapBlock.cpp @@ -918,6 +918,21 @@ HRESULT CLR_RT_HeapBlock::PerformUnboxing(const CLR_RT_TypeDef_Instance &cls) // No luck. The types in src object and specified by cls are different. Need to throw exceptioin. NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_CAST); } + + // now check edge cases + + ////////////////////////////////////////////////////////// + // GUID: can't cast to anything except another GUID object + CLR_RT_TypeDescriptor srcTypeDes; + NANOCLR_CHECK_HRESULT(srcTypeDes.InitializeFromObject(*src)); + + CLR_RT_TypeDef_Instance& inst = srcTypeDes.m_handlerCls; + + if (inst.m_data == g_CLR_RT_WellKnownTypes.m_Guid.m_data) + { + // can't cast GUID class to anything else except another GUID + NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_CAST); + } } if (cls.m_target->dataType == DATATYPE_VALUETYPE) diff --git a/src/CLR/Core/TypeSystem.cpp b/src/CLR/Core/TypeSystem.cpp index 63f43d7976..0d79ca2657 100644 --- a/src/CLR/Core/TypeSystem.cpp +++ b/src/CLR/Core/TypeSystem.cpp @@ -2816,6 +2816,8 @@ static const TypeIndexLookup c_TypeIndexLookup[] = { TIL("System.Reflection", "RuntimeFieldInfo", m_FieldInfo), TIL("System", "WeakReference", m_WeakReference), + + TIL("System", "Guid", m_Guid), TIL("nanoFramework.UI", "Bitmap", m_Bitmap), TIL("nanoFramework.UI", "Font", m_Font), diff --git a/src/CLR/Include/nanoCLR_Runtime.h b/src/CLR/Include/nanoCLR_Runtime.h index baf9507b6f..db6490b36f 100644 --- a/src/CLR/Include/nanoCLR_Runtime.h +++ b/src/CLR/Include/nanoCLR_Runtime.h @@ -1552,6 +1552,8 @@ struct CLR_RT_WellKnownTypes CLR_RT_TypeDef_Index m_WeakReference; + CLR_RT_TypeDef_Index m_Guid; + CLR_RT_TypeDef_Index m_SerializationHintsAttribute; CLR_RT_TypeDef_Index m_Bitmap; CLR_RT_TypeDef_Index m_Font;