From a832153f28aa129e4fa9334a3679d148fb6c214a Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Mon, 4 Aug 2025 17:36:13 +0100 Subject: [PATCH 1/2] Use `string.GetPinnableReference` in fixed where string is not null --- .../src/System/Text/ASCIIEncoding.cs | 4 ++-- .../System.Private.CoreLib/src/System/Text/Encoding.cs | 4 ++-- .../src/System/Text/Latin1Encoding.cs | 8 ++++---- .../src/System/Text/UTF32Encoding.cs | 4 ++-- .../src/System/Text/UTF7Encoding.cs | 4 ++-- .../src/System/Text/UTF8Encoding.Sealed.cs | 2 +- .../src/System/Text/UTF8Encoding.cs | 4 ++-- .../src/System/Text/UnicodeEncoding.cs | 4 ++-- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs index 3aae57e98ef6c1..5bf3d8ab3a3feb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs @@ -111,7 +111,7 @@ public override unsafe int GetByteCount(string chars) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chars); } - fixed (char* pChars = chars) + fixed (char* pChars = &chars.GetPinnableReference()) { return GetByteCountCommon(pChars, chars!.Length); } @@ -232,7 +232,7 @@ public override unsafe int GetBytes(string chars, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = chars) + fixed (char* pChars = &chars.GetPinnableReference()) fixed (byte* pBytes = bytes) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs index e60a5eca24b1c7..3c897be83d3a8c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs @@ -556,7 +556,7 @@ public int GetByteCount(string s, int index, int count) unsafe { - fixed (char* pChar = s) + fixed (char* pChar = &s.GetPinnableReference()) { return GetByteCount(pChar + index, count); } @@ -644,7 +644,7 @@ public byte[] GetBytes(string s, int index, int count) unsafe { - fixed (char* pChar = s) + fixed (char* pChar = &s.GetPinnableReference()) { int byteCount = GetByteCount(pChar + index, count); if (byteCount == 0) diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs index e129b5284bcb2a..f10c33f31bfa6f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs @@ -93,7 +93,7 @@ public override unsafe int GetByteCount(string s) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) { return GetByteCountCommon(pChars, s.Length); } @@ -278,7 +278,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, byte ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) fixed (byte* pBytes = bytes) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); @@ -555,7 +555,7 @@ public override unsafe string GetString(byte[] bytes) string result = string.FastAllocateString(bytes.Length); fixed (byte* pBytes = bytes) - fixed (char* pChars = result) + fixed (char* pChars = &result.GetPinnableReference()) { GetCharsCommon(pBytes, bytes.Length, pChars, result.Length); } @@ -585,7 +585,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) string result = string.FastAllocateString(count); fixed (byte* pBytes = bytes) - fixed (char* pChars = result) + fixed (char* pChars = &result.GetPinnableReference()) { GetCharsCommon(pBytes + index, count, pChars, count); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs index 41031f77344809..99f6e2d0a90e52 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs @@ -121,7 +121,7 @@ public override unsafe int GetByteCount(string s) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) return GetByteCount(pChars, s.Length, null); } @@ -162,7 +162,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, null); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs index 1ae72a0fbd3931..55f00138e60850 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs @@ -157,7 +157,7 @@ public override unsafe int GetByteCount(string s) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) return GetByteCount(pChars, s.Length, null); } @@ -198,7 +198,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, null); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs index a0358cce2a980b..227ec220f6df3c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs @@ -62,7 +62,7 @@ private unsafe byte[] GetBytesForSmallInput(string s) int sourceLength = s.Length; // hoist this to avoid having the JIT auto-insert null checks int bytesWritten; - fixed (char* pSource = s) + fixed (char* pSource = &s.GetPinnableReference()) { bytesWritten = GetBytesCommon(pSource, sourceLength, pDestination, MaxSmallInputElementCount * MaxUtf8BytesPerChar); Debug.Assert(0 <= bytesWritten && bytesWritten <= s.Length * MaxUtf8BytesPerChar); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs index f4c4d4385ada75..8ebeb9fdfa0a79 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs @@ -161,7 +161,7 @@ public override unsafe int GetByteCount(string chars) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chars); } - fixed (char* pChars = chars) + fixed (char* pChars = &chars.GetPinnableReference()) { return GetByteCountCommon(pChars, chars.Length); } @@ -279,7 +279,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) fixed (byte* pBytes = bytes) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs index 7eaf078feae5ad..13a9c8e879d296 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs @@ -113,7 +113,7 @@ public override unsafe int GetByteCount(string s) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) return GetByteCount(pChars, s.Length, null); } @@ -154,7 +154,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, null); From f40ff49c9727fd92355fb968e3e7696b481b7906 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Mon, 4 Aug 2025 18:30:34 +0100 Subject: [PATCH 2/2] Use `MemoryMarshal.GetArrayDataReference` in fixed where array is not null --- .../src/System/Text/ASCIIEncoding.cs | 16 ++++++------- .../src/System/Text/Encoding.cs | 2 +- .../src/System/Text/Latin1Encoding.cs | 24 +++++++++---------- .../src/System/Text/UTF32Encoding.cs | 10 ++++---- .../src/System/Text/UTF7Encoding.cs | 10 ++++---- .../src/System/Text/UTF8Encoding.Sealed.cs | 2 +- .../src/System/Text/UTF8Encoding.cs | 16 ++++++------- .../src/System/Text/UnicodeEncoding.cs | 10 ++++---- 8 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs index 5bf3d8ab3a3feb..58e75f34040b91 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs @@ -93,7 +93,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetByteCountCommon(pChars + index, count); } @@ -233,7 +233,7 @@ public override unsafe int GetBytes(string chars, int charIndex, int charCount, } fixed (char* pChars = &chars.GetPinnableReference()) - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -280,8 +280,8 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = chars) - fixed (byte* pBytes = bytes) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -453,7 +453,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetCharCountCommon(pBytes + index, count); } @@ -571,8 +571,8 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.charIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetCharsCommon(pBytes + byteIndex, byteCount, pChars + charIndex, chars.Length - charIndex); } @@ -747,7 +747,7 @@ public override unsafe string GetString(byte[] bytes, int byteIndex, int byteCou if (byteCount == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return string.CreateStringFromEncoding(pBytes + byteIndex, byteCount, this); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs index 3c897be83d3a8c..d722a8d45cb9de 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs @@ -651,7 +651,7 @@ public byte[] GetBytes(string s, int index, int count) return Array.Empty(); byte[] bytes = new byte[byteCount]; - fixed (byte* pBytes = &bytes[0]) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { int bytesReceived = GetBytes(pChar + index, count, pBytes, byteCount); Debug.Assert(byteCount == bytesReceived); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs index f10c33f31bfa6f..476fd14c3afebc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs @@ -70,7 +70,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetByteCountCommon(pChars + index, count); } @@ -216,8 +216,8 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = chars) - fixed (byte* pBytes = bytes) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -279,7 +279,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, byte } fixed (char* pChars = &s.GetPinnableReference()) - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -442,8 +442,8 @@ public override unsafe char[] GetChars(byte[] bytes) char[] chars = new char[bytes.Length]; - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { GetCharsCommon(pBytes, bytes.Length, pChars, chars.Length); } @@ -477,8 +477,8 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.charIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetCharsCommon(pBytes + byteIndex, byteCount, pChars + charIndex, chars.Length - charIndex); } @@ -509,8 +509,8 @@ public override unsafe char[] GetChars(byte[] bytes, int index, int count) char[] chars = new char[count]; - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { GetCharsCommon(pBytes + index, count, pChars, chars.Length); } @@ -554,7 +554,7 @@ public override unsafe string GetString(byte[] bytes) } string result = string.FastAllocateString(bytes.Length); - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) fixed (char* pChars = &result.GetPinnableReference()) { GetCharsCommon(pBytes, bytes.Length, pChars, result.Length); @@ -584,7 +584,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) } string result = string.FastAllocateString(count); - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) fixed (char* pChars = &result.GetPinnableReference()) { GetCharsCommon(pBytes + index, count, pChars, count); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs index 99f6e2d0a90e52..a8340371134410 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs @@ -105,7 +105,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) return 0; // Just call the pointer version - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) return GetByteCount(pChars + index, count, null); } @@ -205,7 +205,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, // Just call pointer version int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { // Remember that byteCount is # to decode, not size of array. @@ -252,7 +252,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) return 0; // Just call pointer version - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return GetCharCount(pBytes + index, count, null); } @@ -297,7 +297,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, // Just call pointer version int charCount = chars.Length - charIndex; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { // Remember that charCount is # to decode, not size of array @@ -342,7 +342,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) // Avoid problems with empty input buffer if (count == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return string.CreateStringFromEncoding( pBytes + index, count, this); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs index 55f00138e60850..16fa6895649813 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs @@ -141,7 +141,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) return 0; // Just call the pointer version - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) return GetByteCount(pChars + index, count, null); } @@ -241,7 +241,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, // Just call pointer version int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { // Remember that byteCount is # to decode, not size of array. @@ -288,7 +288,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) return 0; // Just call pointer version - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return GetCharCount(pBytes + index, count, null); } @@ -333,7 +333,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, // Just call pointer version int charCount = chars.Length - charIndex; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { // Remember that charCount is # to decode, not size of array @@ -378,7 +378,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) // Avoid problems with empty input buffer if (count == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return string.CreateStringFromEncoding( pBytes + index, count, this); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs index 227ec220f6df3c..d97761bf770c8a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs @@ -140,7 +140,7 @@ private unsafe string GetStringForSmallInput(byte[] bytes) int sourceLength = bytes.Length; // hoist this to avoid having the JIT auto-insert null checks int charsWritten; - fixed (byte* pSource = bytes) + fixed (byte* pSource = &MemoryMarshal.GetArrayDataReference(bytes)) { charsWritten = GetCharsCommon(pSource, sourceLength, pDestination, MaxSmallInputElementCount); Debug.Assert(0 <= charsWritten && charsWritten <= sourceLength); // should never have more output chars than input bytes diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs index 8ebeb9fdfa0a79..e4fb8fa93c328d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs @@ -143,7 +143,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetByteCountCommon(pChars + index, count); } @@ -280,7 +280,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, } fixed (char* pChars = &s.GetPinnableReference()) - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -327,8 +327,8 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = chars) - fixed (byte* pBytes = bytes) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -454,7 +454,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetCharCountCommon(pBytes + index, count); } @@ -522,8 +522,8 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.charIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetCharsCommon(pBytes + byteIndex, byteCount, pChars + charIndex, chars.Length - charIndex); } @@ -697,7 +697,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) if (count == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return string.CreateStringFromEncoding(pBytes + index, count, this); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs index 13a9c8e879d296..2d6ef64c6f4c92 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs @@ -97,7 +97,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) return 0; // Just call the pointer version - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) return GetByteCount(pChars + index, count, null); } @@ -197,7 +197,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, // Just call pointer version int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { // Remember that byteCount is # to decode, not size of array. @@ -244,7 +244,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) return 0; // Just call pointer version - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return GetCharCount(pBytes + index, count, null); } @@ -289,7 +289,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, // Just call pointer version int charCount = chars.Length - charIndex; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { // Remember that charCount is # to decode, not size of array @@ -334,7 +334,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) // Avoid problems with empty input buffer if (count == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return string.CreateStringFromEncoding( pBytes + index, count, this); }