-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Do not clone empty arrays in CloneByteArray #93231
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones Issue Details
If its empty, just return the identity to save the allocation.
|
src/libraries/Common/src/System/Security/Cryptography/Helpers.cs
Outdated
Show resolved
Hide resolved
|
Failure is tracked in #93229. Merging. |
| return src switch | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return (byte[])(src.Clone()); | ||
| null => null, | ||
| { Length: 0 } => src, | ||
| _ => (byte[])src.Clone(), | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, it could also have been:
return src is not null ? src.AsSpan().ToArray() : null;and let span's ToArray handle the empty case via Array.Empty.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

CloneByteArrayis used heavily in S.S.Cryptography to create defensive copies of byte arrays. This is to avoid exposing any kind of mutability to callers.Clonehowever appears to create a newbyte[]for empty byte arrays every time. Empty arrays are not uncommon. It could be for a CNG property, it could be for key parameters, or otherAsnEncodedData.If its empty, just return the identity to save the allocation.