diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanCng.Windows.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanCng.Windows.cs index 4c36b457306c85..c02392df0f300d 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanCng.Windows.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanCng.Windows.cs @@ -41,7 +41,13 @@ protected override partial void DeriveRawSecretAgreementCore(X25519DiffieHellman { // We intentionally don't special case otherParty being an instance of X25519DiffieHellmanCng and always // export the public key into the current instance's provider. - Span publicKeyBuffer = stackalloc byte[PublicKeySizeInBytes * 2]; + scoped Span publicKeyBuffer; + + unsafe + { + publicKeyBuffer = stackalloc byte[PublicKeySizeInBytes * 2]; + } + Span publicKeyBytes = publicKeyBuffer.Slice(0, PublicKeySizeInBytes); Span reducedPublicKey = publicKeyBuffer.Slice(PublicKeySizeInBytes, PublicKeySizeInBytes); otherParty.ExportPublicKey(publicKeyBytes);