Skip to content

Commit 01ead7b

Browse files
committed
replace ineffective array type-checking with simple arrayref pinning
1 parent 3f568fe commit 01ead7b

File tree

1 file changed

+10
-39
lines changed
  • src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging

1 file changed

+10
-39
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapSource.cs

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
using System.IO.Packaging;
2525
using UnsafeNativeMethods = MS.Win32.PresentationCore.UnsafeNativeMethods;
2626
using SR = MS.Internal.PresentationCore.SR;
27-
using MS.Internal.PresentationCore; // SecurityHelper
27+
using MS.Internal.PresentationCore;
28+
using System.Runtime.CompilerServices;
2829

2930
namespace System.Windows.Media.Imaging
3031
{
@@ -657,7 +658,7 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
657658
ArgumentNullException.ThrowIfNull(pixels);
658659

659660
if (pixels.Rank != 1)
660-
throw new ArgumentException(SR.Collection_BadRank, "pixels");
661+
throw new ArgumentException(SR.Collection_BadRank, nameof(pixels));
661662

662663
if (offset < 0)
663664
{
@@ -680,43 +681,13 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
680681

681682
int destBufferSize = checked(elementSize * (pixels.Length - offset));
682683

684+
// Check whether offset is out of bounds manually
685+
if (offset >= pixels.Length)
686+
throw new IndexOutOfRangeException();
683687

684-
if (pixels is byte[])
685-
{
686-
fixed (void* pixelArray = &((byte[])pixels)[offset])
687-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
688-
}
689-
else if (pixels is short[])
690-
{
691-
fixed (void* pixelArray = &((short[])pixels)[offset])
692-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
693-
}
694-
else if (pixels is ushort[])
695-
{
696-
fixed (void* pixelArray = &((ushort[])pixels)[offset])
697-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
698-
}
699-
else if (pixels is int[])
700-
{
701-
fixed (void* pixelArray = &((int[])pixels)[offset])
702-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
703-
}
704-
else if (pixels is uint[])
705-
{
706-
fixed (void* pixelArray = &((uint[])pixels)[offset])
707-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
708-
}
709-
else if (pixels is float[])
710-
{
711-
fixed (void* pixelArray = &((float[])pixels)[offset])
712-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
713-
}
714-
else if (pixels is double[])
715-
{
716-
fixed (void* pixelArray = &((double[])pixels)[offset])
717-
CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride);
718-
}
719-
}
688+
fixed (byte* pixelArray = &Unsafe.AddByteOffset(ref MemoryMarshal.GetArrayDataReference(pixels), (nint)offset * elementSize))
689+
CriticalCopyPixels(sourceRect, (nint)pixelArray, destBufferSize, stride);
690+
}
720691

721692
/// <summary>
722693
/// CriticalCopyPixels
@@ -728,7 +699,7 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
728699
internal void CriticalCopyPixels(Int32Rect sourceRect, IntPtr buffer, int bufferSize, int stride)
729700
{
730701
if (buffer == IntPtr.Zero)
731-
throw new ArgumentNullException("buffer");
702+
throw new ArgumentNullException(nameof(buffer));
732703

733704
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(stride);
734705

0 commit comments

Comments
 (0)