From 57c8504b2095d15f2d70044a6cdc6b34b3f7c3ab Mon Sep 17 00:00:00 2001 From: =?utf8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Wed, 8 Mar 2023 23:19:04 +0100 Subject: [PATCH] Fix undefined behaviour in Array.cs (#83116) Fixes #83115 --- src/libraries/System.Private.CoreLib/src/System/Array.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Array.cs b/src/libraries/System.Private.CoreLib/src/System/Array.cs index a937f69..9772545 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Array.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Array.cs @@ -1332,7 +1332,7 @@ namespace System if (sizeof(T) == sizeof(byte)) { int result = SpanHelpers.IndexOfValueType( - ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), startIndex), + ref Unsafe.Add(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(array)), startIndex), Unsafe.As(ref value), count); return (result >= 0 ? startIndex : 0) + result; @@ -1340,7 +1340,7 @@ namespace System else if (sizeof(T) == sizeof(short)) { int result = SpanHelpers.IndexOfValueType( - ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), startIndex), + ref Unsafe.Add(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(array)), startIndex), Unsafe.As(ref value), count); return (result >= 0 ? startIndex : 0) + result; @@ -1348,7 +1348,7 @@ namespace System else if (sizeof(T) == sizeof(int)) { int result = SpanHelpers.IndexOfValueType( - ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), startIndex), + ref Unsafe.Add(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(array)), startIndex), Unsafe.As(ref value), count); return (result >= 0 ? startIndex : 0) + result; @@ -1356,7 +1356,7 @@ namespace System else if (sizeof(T) == sizeof(long)) { int result = SpanHelpers.IndexOfValueType( - ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), startIndex), + ref Unsafe.Add(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(array)), startIndex), Unsafe.As(ref value), count); return (result >= 0 ? startIndex : 0) + result; @@ -1580,7 +1580,7 @@ namespace System { int endIndex = startIndex - count + 1; int result = SpanHelpers.LastIndexOfValueType( - ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), endIndex), + ref Unsafe.Add(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(array)), endIndex), Unsafe.As(ref value), count); @@ -1590,7 +1590,7 @@ namespace System { int endIndex = startIndex - count + 1; int result = SpanHelpers.LastIndexOfValueType( - ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), endIndex), + ref Unsafe.Add(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(array)), endIndex), Unsafe.As(ref value), count); @@ -1600,7 +1600,7 @@ namespace System { int endIndex = startIndex - count + 1; int result = SpanHelpers.LastIndexOfValueType( - ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), endIndex), + ref Unsafe.Add(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(array)), endIndex), Unsafe.As(ref value), count); @@ -1610,7 +1610,7 @@ namespace System { int endIndex = startIndex - count + 1; int result = SpanHelpers.LastIndexOfValueType( - ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), endIndex), + ref Unsafe.Add(ref Unsafe.As(ref MemoryMarshal.GetArrayDataReference(array)), endIndex), Unsafe.As(ref value), count); -- 2.7.4