From 8e853621b58ce83cc467d48714910fd6711725ef Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Mon, 9 Oct 2023 11:02:36 -0400 Subject: [PATCH 1/2] Do not clone empty arrays in CloneByteArray --- .../Common/src/System/Security/Cryptography/Helpers.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/Common/src/System/Security/Cryptography/Helpers.cs b/src/libraries/Common/src/System/Security/Cryptography/Helpers.cs index b1dc4d1de9a04..dc8c724edad63 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Helpers.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/Helpers.cs @@ -45,12 +45,12 @@ internal static partial class Helpers [return: NotNullIfNotNull(nameof(src))] public static byte[]? CloneByteArray(this byte[]? src) { - if (src == null) + return src switch { - return null; - } - - return (byte[])(src.Clone()); + null => null, + [] => src, + _ => (byte[])src.Clone(), + }; } internal static bool TryCopyToDestination(this ReadOnlySpan source, Span destination, out int bytesWritten) From e857699a60d8d83fa8c044107ca868ef688f586e Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Mon, 9 Oct 2023 12:19:57 -0400 Subject: [PATCH 2/2] Fix compilation. --- .../Common/src/System/Security/Cryptography/Helpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Common/src/System/Security/Cryptography/Helpers.cs b/src/libraries/Common/src/System/Security/Cryptography/Helpers.cs index dc8c724edad63..27dc3a2e79674 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Helpers.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/Helpers.cs @@ -48,7 +48,7 @@ internal static partial class Helpers return src switch { null => null, - [] => src, + { Length: 0 } => src, _ => (byte[])src.Clone(), }; }