Skip to content

Conversation

@vcsjones
Copy link
Member

A few more small allocations that can be avoided, and a little bit of formatting cleanup in X509Certificate2.

@ghost ghost added the area-System.Security label May 25, 2022
@ghost ghost assigned vcsjones May 25, 2022
@ghost
Copy link

ghost commented May 25, 2022

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

Issue Details

A few more small allocations that can be avoided, and a little bit of formatting cleanup in X509Certificate2.

Author: vcsjones
Assignees: -
Labels:

area-System.Security

Milestone: -

string keyAlgorithmOid = GetKeyAlgorithm();
byte[] parameters = GetKeyAlgorithmParameters();
byte[] keyValue = GetPublicKey();
byte[] parameters = Pal.KeyAlgorithmParameters;
Copy link
Member Author

Choose a reason for hiding this comment

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

The public APIs GetKeyAlgorithmParameters and GetPublicKey create defensive copies, then we were feeding them in to AsnEncodedData, which again created defensive copies. If we go to the PAL directly for the bytes we can avoid the first defensive copy.

private SafeCreateHandle PreparePoliciesArray(bool checkRevocation)
{
IntPtr[] policies = new IntPtr[checkRevocation ? 2 : 1];
Span<IntPtr> policies = checkRevocation ? stackalloc IntPtr[2] : stackalloc IntPtr[1];
Copy link
Member Author

@vcsjones vcsjones May 25, 2022

Choose a reason for hiding this comment

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

It's okay to put stack data in to CFArrayCreate as long as the items themselves are not from the stack. It creates a CoreFoundation array from the contents of the stack, it doesn't try to take ownership of the stack data.

We're also doing this in SafeDeleteSslContext as well.

Span<IntPtr> handles = certList.Count <= 256
? stackalloc IntPtr[256]
: new IntPtr[certList.Count];

@bartonjs bartonjs merged commit 7f7c6a2 into dotnet:main May 27, 2022
@vcsjones vcsjones deleted the small-alloc-fix branch May 27, 2022 16:08
@ghost ghost locked as resolved and limited conversation to collaborators Jun 26, 2022
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.

2 participants