Commit a67d388
Remove unsafe code from SmtpReplyReaderFactory.ProcessRead (#121297)
The `ProcessRead` method used unnecessary unsafe pointer arithmetic
despite accepting a `ReadOnlySpan<byte>` parameter that already provides
safe, efficient indexing.
## Changes
- **Replaced unsafe pointer arithmetic with ReadOnlySpan indexing**
- Removed `unsafe` block and `fixed` statement
- Replaced `byte*` pointers with integer index tracking
- Changed `*ptr++` to `buffer[i++]`, `(int)(ptr - start)` to `i`
- **Fixed pre-existing validation bug** (discovered during refactoring)
- Changed `if (b < '0' && b > '9')` to `if (b < '0' || b > '9')` in
digit validation
- Original condition could never be true (no byte can be both < 48 and >
57)
## Before/After
```csharp
// Before
unsafe {
fixed (byte* pBuffer = buffer) {
byte* ptr = pBuffer;
byte b = *ptr++;
if (b < '0' && b > '9') // Bug: never true
throw new FormatException(...);
}
}
// After
int i = 0;
byte b = buffer[i++];
if (b < '0' || b > '9') // Correct validation
throw new FormatException(...);
```
All existing tests pass. No API or behavior changes.
> [!WARNING]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### I tried to connect to the following addresses, but was blocked by
firewall rules:
>
> - `1d7b368770e34adf874f9425defe0a54`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `2f1ac5ebdede409c885709296cd79caf`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `3e267405dc55499fbf89ba600b269486`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `4741d8119c5b46deb7b6a3247cf3e987`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `4777e169b2a54bef872d3013b91d119d`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `674cfdb34af049bc80dcbc8c430c56e8`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `83b38af9921845538f0d2b8e91bd4c93`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `8445b7f8594e4fa79d67a5f60e1917e1`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `9c17f6fe624d4688a482d039f62e9376`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `9caf23efb9b44deeaad1f5a4390857ed`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `ad5b1d9a0284470497198faf9964749f`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `dad9f17011b54a2883ca8f7489dde4e8`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `dc00558270b44330978820a282438ebc`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `f13d3b87148d4771ab29f5a3afc20964`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
> - `f4e51707a3454d68a871b21a5edce6f6`
> - Triggering command:
`/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet
exec --runtimeconfig System.Net.Mail.Functional.Tests.runtimeconfig.json
--depsfile System.Net.Mail.Functional.Tests.deps.json
/home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.25528.108/build/../tools/net/xunit.console.dll
System.Net.Mail.Functional.Tests.dll -xml testResults.xml -nologo
-notrait category=OuterLoop -notrait category=failing` (dns block)
>
> If you need me to access, download, or install something from one of
these locations, you can either:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/dotnet/runtime/settings/copilot/coding_agent)
(admins only)
>
> </details>
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
> There is absolutely unnecessary unsafe code in
`SmtpReplyReaderFactory.cs` in `ProcessRead` function, please file a PR
to replace with just normal ReadOnlySpan memory access to make it memory
safe.
</details>
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Co-authored-by: Egor Bogatov <egorbo@gmail.com>
Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com>1 parent 767be2a commit a67d388
File tree
1 file changed
+116
-124
lines changed- src/libraries/System.Net.Mail/src/System/Net/Mail
1 file changed
+116
-124
lines changedLines changed: 116 additions & 124 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
92 | 95 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
98 | 107 | | |
99 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
100 | 114 | | |
101 | | - | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
102 | 119 | | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
| 120 | + | |
117 | 121 | | |
118 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
119 | 153 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
| 154 | + | |
134 | 155 | | |
135 | | - | |
| 156 | + | |
136 | 157 | | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
| 158 | + | |
151 | 159 | | |
152 | | - | |
| 160 | + | |
153 | 161 | | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
| 162 | + | |
172 | 163 | | |
173 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
174 | 173 | | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
| 174 | + | |
184 | 175 | | |
185 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
186 | 185 | | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
| 186 | + | |
202 | 187 | | |
203 | | - | |
| 188 | + | |
204 | 189 | | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
| 190 | + | |
| 191 | + | |
214 | 192 | | |
215 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
216 | 203 | | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
| 204 | + | |
227 | 205 | | |
228 | | - | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
229 | 215 | | |
230 | | - | |
231 | | - | |
232 | | - | |
| 216 | + | |
233 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
234 | 227 | | |
235 | | - | |
236 | | - | |
237 | 228 | | |
| 229 | + | |
238 | 230 | | |
239 | 231 | | |
240 | 232 | | |
| |||
0 commit comments