Fix CS9361 stackalloc unsafe context in X25519DiffieHellmanCng#127999
Fix CS9361 stackalloc unsafe context in X25519DiffieHellmanCng#127999
Conversation
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/93cdfa67-d51d-4448-89a2-bbff16671e8b Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
unsafe context for stackalloc in X25519DiffieHellmanCng to satisfy CS9361
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
|
@copilot fix X25519DiffieHellmanCng.Windows.cs |
…instead of unsafe block Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/728f2abc-f832-4d66-9f31-57620b3d0c6a Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
unsafe context for stackalloc in X25519DiffieHellmanCng to satisfy CS9361unsafe modifier to DeriveRawSecretAgreementCore to satisfy CS9361
Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
unsafe modifier to DeriveRawSecretAgreementCore to satisfy CS9361There was a problem hiding this comment.
Pull request overview
This PR fixes a build break in System.Security.Cryptography caused by compiler error CS9361 when using uninitialized stackalloc in a project with assembly-wide [SkipLocalsInit]. The change keeps the method itself in safe code while placing only the stackalloc expression into a narrowly-scoped unsafe block, preserving the original span ref-safety via scoped.
Changes:
- Replaces
Span<byte> publicKeyBuffer = stackalloc ...with ascoped Span<byte>local. - Wraps the uninitialized
stackallocassignment in a minimalunsafe { ... }block to satisfy CS9361.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanCng.Windows.cs | Moves the stackalloc into an unsafe block and uses scoped Span<byte> to retain safe ref-safety while fixing CS9361 under [SkipLocalsInit]. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 0
|
/ba-g browser-wasm leg timed out. This change does not affect browser wasm, only windows, and all Windows legs passed. Merging to fix build break. |
|
Okay, |
|
/ba-g |
The new Roslyn shipped in #127944 (5.7.0-1.26257.113) introduced
CS9361, which requires anunsafecontext forstackallocwithout an initializer when[SkipLocalsInit]is in effect (assembly-wide forNetCoreAppLibraryprojects viasrc/libraries/Directory.Build.targets). This broke the build of System.Security.Cryptography onX25519DiffieHellmanCng.DeriveRawSecretAgreementCore, recently added in fe45871.Description
In
X25519DiffieHellmanCng.Windows.cs, declarepublicKeyBufferasscoped Span<byte>and wrap only thestackallocin a narrowly-scopedunsafeblock, rather than marking the whole methodunsafe:The
scopedmodifier preserves the safe ref-safety semantics of the originalSpan<byte> publicKeyBuffer = stackalloc ...declaration so nounsafemodifier is required on the partial method itself.Audited other
Span<T> = stackalloc T[N]sites in projects with globalSkipLocalsInit. CS9361 only fires on this one site; existing call sites are either already onunsafemethods (e.g.AesImplementation.GenerateKey,Aes.Rfc3394Wrap) or are not flagged by Roslyn under current configuration. The CI logs for Optimize Enumerable Min/Max final reduction with shuffles #127995 confirm a single failing location.