Remove unsafe code from IBinaryInteger.TryReadBigEndian implementations#127485
Merged
EgorBo merged 3 commits intodotnet:mainfrom Apr 28, 2026
Merged
Remove unsafe code from IBinaryInteger.TryReadBigEndian implementations#127485EgorBo merged 3 commits intodotnet:mainfrom
EgorBo merged 3 commits intodotnet:mainfrom
Conversation
Replace Unsafe.ReadUnaligned/Unsafe.Add/MemoryMarshal.GetReference usage in TryReadBigEndian on Byte, SByte, Char, Int16/UInt16, Int32/UInt32, Int64/UInt64, IntPtr/UIntPtr, Int128/UInt128 with safe equivalents (BinaryPrimitives.ReadXxxBigEndian and span indexing). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
Author
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors IBinaryInteger<T>.TryReadBigEndian implementations in System.Private.CoreLib primitive / intrinsic numeric types to avoid Unsafe.*-based unaligned reads and pointer arithmetic, using BinaryPrimitives.Read*BigEndian and ReadOnlySpan<byte> indexing instead.
Changes:
- Replaced
Unsafe.ReadUnaligned+ manual endianness reversal withBinaryPrimitives.Read{U}Int{16|32|64|128}BigEndianon a tail slice of the input span. - Replaced
Unsafe.Add(ref sourceRef, i)byte iteration withsource[i]indexing in the small-length construction loops. - Updated
nint/nuintbig-endian reads to useBinaryPrimitives.ReadInt{32|64}BigEndian/ReadUInt{32|64}BigEndianunder#if TARGET_64BIT.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs | Switches big-endian read to `BinaryPrimitives.ReadUInt{32 |
| src/libraries/System.Private.CoreLib/src/System/UInt64.cs | Uses BinaryPrimitives.ReadUInt64BigEndian and span indexing for fallback path. |
| src/libraries/System.Private.CoreLib/src/System/UInt32.cs | Uses BinaryPrimitives.ReadUInt32BigEndian and span indexing for fallback path. |
| src/libraries/System.Private.CoreLib/src/System/UInt16.cs | Uses BinaryPrimitives.ReadUInt16BigEndian and source[0] for the 1-byte case. |
| src/libraries/System.Private.CoreLib/src/System/UInt128.cs | Uses BinaryPrimitives.ReadUInt128BigEndian and span indexing for fallback path. |
| src/libraries/System.Private.CoreLib/src/System/SByte.cs | Replaces unsafe last-byte access with span indexing for big-endian 1-byte read. |
| src/libraries/System.Private.CoreLib/src/System/IntPtr.cs | Switches big-endian read to `BinaryPrimitives.ReadInt{32 |
| src/libraries/System.Private.CoreLib/src/System/Int64.cs | Uses BinaryPrimitives.ReadInt64BigEndian and span indexing for fallback path. |
| src/libraries/System.Private.CoreLib/src/System/Int32.cs | Uses BinaryPrimitives.ReadInt32BigEndian and span indexing for fallback path. |
| src/libraries/System.Private.CoreLib/src/System/Int16.cs | Uses BinaryPrimitives.ReadInt16BigEndian and source[0] for the 1-byte cases (signed/unsigned). |
| src/libraries/System.Private.CoreLib/src/System/Int128.cs | Uses BinaryPrimitives.ReadInt128BigEndian and span indexing for fallback path. |
| src/libraries/System.Private.CoreLib/src/System/Char.cs | Uses BinaryPrimitives.ReadUInt16BigEndian (cast to char) and source[0] for the 1-byte case. |
| src/libraries/System.Private.CoreLib/src/System/Byte.cs | Replaces unsafe last-byte access with span indexing for big-endian 1-byte read. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This was referenced Apr 28, 2026
Open
Member
Author
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces
Unsafe.ReadUnaligned/Unsafe.Add/MemoryMarshal.GetReferenceusage inIBinaryInteger<T>.TryReadBigEndian(Byte, SByte, Char, Int16/UInt16, Int32/UInt32, Int64/UInt64, IntPtr/UIntPtr, Int128/UInt128) with safe equivalents (BinaryPrimitives.ReadXxxBigEndianand span indexing). The JIT is expected to eliminate the resulting bounds checks given the existing length gates.Note
This PR was authored with assistance from Copilot CLI.
UPD: blocked by #127486(improved in JIT via #127488)Diffs