Skip to content

Remove a few unsafe bits in S.P.Uri, S.N.Ping, System.Transactions.Local #116281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2025

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Jun 3, 2025

diffs (size regression in ParseCanonicalName is due to inlining of Parse)

Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@EgorBo EgorBo marked this pull request as ready for review June 3, 2025 22:53
@Copilot Copilot AI review requested due to automatic review settings June 3, 2025 22:53
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR removes several unsafe code paths across three libraries by adopting safer constructs and modern C# features.

  • Replace fixed buffers in Xactopt marshalling with InlineArray and safe spans
  • Refactor IPv4AddressHelper.ParseCanonicalName to eliminate pointer math and use spans
  • Remove unsafe methods in Ping.RawSocket, replacing pointer-based socket options with spans

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
System/Transactions/DtcProxyShim/Xactopt.cs Switched from unsafe fixed byte buffer to InlineArray, removed unsafe from marshaller methods
System.Private.Uri/src/System/UriExt.cs Dropped unused unsafe modifier from GetRelativeSerializationString
System.Private.Uri/src/System/IPv4AddressHelper.cs Rewrote ParseCanonicalName to use spans instead of stackalloc pointers and removed obsolete Parse
System.Net.Ping/src/System/Net/NetworkInformation/Ping.RawSocket.cs Removed unsafe contexts in raw socket setup and replaced pointer options with ReadOnlySpan<byte>
Comments suppressed due to low confidence (2)

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.RawSocket.cs:109

  • [nitpick] The variable name opt is ambiguous. Consider a more descriptive name like recvErrOptionValue or optionBytes to clarify its purpose.
ReadOnlySpan<byte> opt = BitConverter.IsLittleEndian ? [1, 0, 0, 0] : [0, 0, 0, 1];

src/libraries/System.Private.Uri/src/System/IPv4AddressHelper.cs:30

  • This array literal creates a heap-allocated array before converting to a Span. For a small fixed-size buffer, consider using stackalloc, e.g.: Span<byte> numbers = stackalloc byte[NumberOfLabels] { ... }; to avoid heap allocations.
Span<byte> numbers =

@EgorBo
Copy link
Member Author

EgorBo commented Jun 4, 2025

@MihuBot

@EgorBo EgorBo merged commit d1ec77f into dotnet:main Jun 5, 2025
86 checks passed
@EgorBo EgorBo deleted the unsafe-5 branch June 5, 2025 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants