Skip to content

Removed unsafe string mutation #31850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2021
Merged

Conversation

gfoidl
Copy link
Member

@gfoidl gfoidl commented Apr 15, 2021

Addresses #31821

I didn't ran benchmarks, as it's basically the same as in #17556 and there this pattern is an improvement.

These should cover all occurances of mutating strings, based on new string\(('\\0'|\(char\)0)-search.

/cc: @GrabYourPitchforks PTAL

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Apr 15, 2021
@@ -65,25 +65,22 @@ public BCryptKeyHandle GenerateSymmetricKey(byte* pbSecret, uint cbSecret)
/// </summary>
public string GetAlgorithmName()
{
const int StackAllocCharSize = 128;
Copy link
Member Author

Choose a reason for hiding this comment

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

There can't be longer algorithm names?
I gues no, but ... want to double-check.

Copy link
Member

Choose a reason for hiding this comment

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

I'd be extremely surprised if that were the case. So what you had proposed should be good.

If you wanted to be defensive you could always string.Create instead of stackalloc, but overflowing this buffer is never going to happen in practice. (This method isn't even called outside unit tests TBH.)

@@ -70,27 +67,59 @@ private static unsafe void GetAsciiStringNonNullCharacters(Span<char> buffer, In
}
}

private static readonly SpanAction<char, IntPtr> s_getAsciiStringNonNullCharacters = GetAsciiStringNonNullCharacters;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Can we move the static SpanAction fields to the top of the class?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done with efe80e7


A note for the rationale behind the placement:
As these delegates are used in only one special place (to avoid the compiler generating the cachable display-class and the null-check) these were placed near to the usage to have "compact code".

@halter73 halter73 merged commit a4bc912 into dotnet:main Apr 22, 2021
@ghost ghost added this to the 6.0-preview5 milestone Apr 22, 2021
@halter73
Copy link
Member

Thanks @gfoidl!

@gfoidl gfoidl deleted the dont-mutate-strings branch April 22, 2021 07:47
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dataprotection Includes: DataProtection area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions community-contribution Indicates that the PR has been added by a community member feature-kestrel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants