Skip to content

Commit

Permalink
UnBoxEachElement only allows exact type match
Browse files Browse the repository at this point in the history
  • Loading branch information
huoyaoyuan committed Jan 24, 2024
1 parent 79ff323 commit 491dd2e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,23 @@ private static unsafe void CopyImplUnBoxEachElement(Array sourceArray, int sourc
pDestMT,
obj);
}
else if (obj is null)
else if (obj is null || RuntimeHelpers.GetMethodTable(obj) != pDestMT)
{
GC.KeepAlive(obj);
ThrowHelper.ThrowInvalidCastException_DownCastArrayElement();
}
else if (pDestMT->ContainsGCPointers)
{
Buffer.BulkMoveWithWriteBarrier(
ref dest,
ref CastHelpers.Unbox(pDestMT, obj),
ref obj.GetRawData(),
destSize);
}
else
{
Buffer.Memmove(
ref dest,
ref CastHelpers.Unbox(pDestMT, obj),
ref obj.GetRawData(),
destSize);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ internal static unsafe class CastHelpers
[DebuggerHidden]
[StackTraceHidden]
[DebuggerStepThrough]
internal static ref byte Unbox(void* toTypeHnd, object obj)
private static ref byte Unbox(void* toTypeHnd, object obj)
{
// this will throw NullReferenceException if obj is null, attributed to the user code, as expected.
if (RuntimeHelpers.GetMethodTable(obj) == toTypeHnd)
Expand Down

0 comments on commit 491dd2e

Please sign in to comment.