Repository navigation
Remove unsafe code from System.Net.Mail decoders - #130743
Conversation
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
This comment was marked as low quality.
This comment was marked as low quality.
|
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. |
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
There was a problem hiding this comment.
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
ReadStatetracking.
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
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
|
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 |
MihaZupan
left a comment
There was a problem hiding this comment.
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
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.