-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
Strings in .NET are immutable, and we want to experiment with runtime features (deduplication, codegen optimizations, etc.) which rely on this fact. To accomplish this we'll need to start moving away from existing code which mutates string instances in-place. Here's a first-pass at all the places in this repo which mutate strings and recommended workarounds for them.
aspnetcore/src/Shared/ServerInfrastructure/StringUtilities.cs
Lines 73 to 94 in 8f56489
public static unsafe string GetLatin1StringNonNullCharacters(this ReadOnlySpan<byte> span) | |
{ | |
if (span.IsEmpty) | |
{ | |
return string.Empty; | |
} | |
var resultString = new string('\0', span.Length); | |
fixed (char* output = resultString) | |
fixed (byte* buffer = span) | |
{ | |
// This returns false if there are any null (0 byte) characters in the string. | |
if (!TryGetLatin1String(buffer, output, span.Length)) | |
{ | |
// null characters are considered invalid | |
throw new InvalidOperationException(); | |
} | |
} | |
return resultString; | |
} |
Suggested workaround (pseudocode):
fixed (byte* pBytes = bytes)
{
return string.Create(bytes.Length, (IntPtr)pBytes, (span, ptr) =>
{
fixed (char* pChars = span)
{
WidenAndCopy((byte*)ptr, pChars, span.Length);
}
});
}
aspnetcore/src/DataProtection/Cryptography.Internal/src/SafeHandles/BCryptAlgorithmHandle.cs
Lines 78 to 84 in 28a0a53
// Allocate a string object and write directly into it (CLR team approves of this mechanism). | |
string retVal = new String((char)0, checked((int)numCharsWithoutNull)); | |
uint numBytesCopied; | |
fixed (char* pRetVal = retVal) | |
{ | |
numBytesCopied = GetProperty(Constants.BCRYPT_ALGORITHM_NAME, pRetVal, byteLengthOfNameWithTerminatingNull); | |
} |
Suggested workaround: Write to a stackalloced buffer, or rent a char array and write to that, then turn it into a string. Or use string.Create
and avoid the intermediate rentals.