Skip to content

Remove unsafe code from System.Net.Mail decoders - #130743

Merged
EgorBo merged 3 commits into
mainfrom
remove-unsafe-mail-decoders
Jul 15, 2026
Merged

EgorBo merged 3 commits into
mainfrom
remove-unsafe-mail-decoders

Conversation

@EgorBo

@EgorBo EgorBo commented Jul 15, 2026 •

Copy link
Copy Markdown
Member

Remove unsafe code in System.Net.Mail. I'm assuming this code doesn't need every single bit of perf, so a couple of redundant bound checks won't hurt.

I recommend reviewing with whitespaces disabled.

Note

This pull request description was generated with GitHub Copilot.

Replace pointer-based in-place decoding with bounded span indexing for Base64 and quoted-printable streams.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: ea009699-545a-4f7b-a4c6-6b040eaee50b
Copilot AI review requested due to automatic review settings July 15, 2026 00:13
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:13 — with GitHub Actions Inactive
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:13 — with GitHub Actions Inactive
@EgorBo

This comment was marked as low quality.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@EgorBo
EgorBo had a problem deploying to copilot-pat-pool July 15, 2026 00:14 — with GitHub Actions Failure
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:16 — with GitHub Actions Inactive
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:16 — with GitHub Actions Inactive
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:16 — with GitHub Actions Inactive

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 refactors the in-place decoding routines used by System.Net.Mail MIME streams to remove unsafe pointer-based loops, replacing them with bounded Span<byte> indexing while preserving the existing “decode into the same buffer” behavior.

Changes:

  • Rewrote DecodeBytes(Span<byte>) implementations to use integer source/destination indices instead of pinned pointers.
  • Added explicit empty-buffer fast paths for quoted-printable and Q-encoded decoding.
  • Preserved partial-escape continuation behavior via existing ReadState tracking.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/libraries/System.Net.Mail/src/System/Net/Mime/QuotedPrintableStream.cs Replaces unsafe pointer-based quoted-printable decoding with bounded Span<byte> indexing.
src/libraries/System.Net.Mail/src/System/Net/Mime/QEncodedStream.cs Replaces unsafe pointer-based Q-encoding decoding with bounded Span<byte> indexing (incl. _ → space handling).
src/libraries/System.Net.Mail/src/System/Net/Base64Stream.cs Replaces unsafe pointer-based base64 decoding loop with Span<byte> indexing while keeping in-place decode semantics.

Address review feedback by formatting the original malformed input character instead of the decoded 255 sentinel.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: ea009699-545a-4f7b-a4c6-6b040eaee50b
Copilot AI review requested due to automatic review settings July 15, 2026 00:23
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:23 — with GitHub Actions Inactive
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:24 — with GitHub Actions Inactive
@EgorBo
EgorBo had a problem deploying to copilot-pat-pool July 15, 2026 00:25 — with GitHub Actions Failure
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:26 — with GitHub Actions Inactive
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:26 — with GitHub Actions Inactive
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:26 — with GitHub Actions Inactive

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Add split quoted-printable decode coverage and hoist the current Base64 source byte to avoid repeated indexing.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: ea009699-545a-4f7b-a4c6-6b040eaee50b
Copilot AI review requested due to automatic review settings July 15, 2026 00:33
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:33 — with GitHub Actions Inactive
@EgorBo
EgorBo temporarily deployed to copilot-pat-pool July 15, 2026 00:33 — with GitHub Actions Inactive

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

@EgorBo

EgorBo commented Jul 15, 2026

Copy link
Copy Markdown
Member Author

PTAL @MihaZupan (I recommend disabling whitespaces on the review page). I assumed this code is not perf critical as jit cannot remove all the bound checks from it

@EgorBo
EgorBo requested a review from MihaZupan July 15, 2026 10:11

@MihaZupan MihaZupan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Plenty of (pre-existing) opportunity for improvement in this logic, but the transformation looks correct to me, thanks.

I assumed this code is not perf critical

Correct

@EgorBo
EgorBo merged commit 3c1a2e6 into main Jul 15, 2026
97 of 98 checks passed
@EgorBo
EgorBo deleted the remove-unsafe-mail-decoders branch July 15, 2026 11:39
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 16, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 16, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — c271d828 Deployed Jul 15, 2026 by EgorBo via conclusion #15059
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants