Skip to content

Commit 9892998

Browse files
authored
Fix box/unbox of generic types (#3237)
***NO_CI***
1 parent a0b3e1d commit 9892998

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/CLR/Core/Interpreter.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2905,7 +2905,28 @@ HRESULT CLR_RT_Thread::Execute_IL(CLR_RT_StackFrame &stackArg)
29052905
}
29062906
else
29072907
{
2908-
NANOCLR_CHECK_HRESULT(evalPos[0].PerformBoxing(typeInst));
2908+
// Check if we're trying to box a null reference for a reference type
2909+
// For generic types, the value might already be null (DATATYPE_OBJECT with null reference)
2910+
if (evalPos[0].DataType() == DATATYPE_OBJECT)
2911+
{
2912+
CLR_RT_HeapBlock *ptr = evalPos[0].Dereference();
2913+
if (ptr == nullptr)
2914+
{
2915+
// Already a null reference, no need to box
2916+
// Just ensure it stays as a null object reference
2917+
evalPos[0].SetObjectReference(nullptr);
2918+
}
2919+
else
2920+
{
2921+
// Non-null object reference, perform boxing
2922+
NANOCLR_CHECK_HRESULT(evalPos[0].PerformBoxing(typeInst));
2923+
}
2924+
}
2925+
else
2926+
{
2927+
// Value type or other type, perform normal boxing
2928+
NANOCLR_CHECK_HRESULT(evalPos[0].PerformBoxing(typeInst));
2929+
}
29092930
}
29102931
}
29112932
else
@@ -2947,6 +2968,17 @@ HRESULT CLR_RT_Thread::Execute_IL(CLR_RT_StackFrame &stackArg)
29472968
}
29482969
else
29492970
{
2971+
// Check if we're trying to unbox a null reference
2972+
if (evalPos[0].DataType() == DATATYPE_OBJECT)
2973+
{
2974+
CLR_RT_HeapBlock *ptr = evalPos[0].Dereference();
2975+
if (ptr == nullptr)
2976+
{
2977+
// Attempting to unbox a null reference throws NullReferenceException
2978+
NANOCLR_SET_AND_LEAVE(CLR_E_NULL_REFERENCE);
2979+
}
2980+
}
2981+
29502982
NANOCLR_CHECK_HRESULT(evalPos[0].PerformUnboxing(typeInst));
29512983
}
29522984
}

0 commit comments

Comments
 (0)