Uh oh!
There was an error while loading. Please reload this page.
Do not clone empty arrays in CloneByteArray - #93231
Conversation
ghost
commented
Oct 9, 2023
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones Issue Details
If its empty, just return the identity to save the allocation.
|
Uh oh!
There was an error while loading. Please reload this page.
vcsjones
commented
Oct 9, 2023
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.
FWIW, it could also have been:
returnsrcis not null?src.AsSpan().ToArray():null;and let span's ToArray handle the empty case via Array.Empty.
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.