From aed6945f071cbea9e5f9cbd1f6ffa0d04b06b924 Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Mon, 19 Sep 2022 13:54:16 -0400 Subject: [PATCH 1/2] Use CommonPrefixLength for SetOf sort validation. --- .../src/System/Formats/Asn1/SetOfValueComparer.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/SetOfValueComparer.cs b/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/SetOfValueComparer.cs index e85db7683c930..648b6a33ab820 100644 --- a/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/SetOfValueComparer.cs +++ b/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/SetOfValueComparer.cs @@ -17,6 +17,18 @@ internal static int Compare(ReadOnlySpan x, ReadOnlySpan y) int min = Math.Min(x.Length, y.Length); int diff; +#if NET7_0_OR_GREATER + int diffIndex = x.Slice(0, min).CommonPrefixLength(y.Slice(0, min)); + + if (diffIndex == min) + { + diff = 0; + } + else + { + return (int)x[diffIndex] - y[diffIndex]; + } +#else for (int i = 0; i < min; i++) { int xVal = x[i]; @@ -28,6 +40,7 @@ internal static int Compare(ReadOnlySpan x, ReadOnlySpan y) return diff; } } +#endif // The sorting rules (T-REC-X.690-201508 sec 11.6) say that the shorter one // counts as if it are padded with as many 0x00s on the right as required for From f95f0c06823bb407717434ee31a68ab06b889dbb Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Mon, 19 Sep 2022 14:38:27 -0400 Subject: [PATCH 2/2] Code review feedback --- .../src/System/Formats/Asn1/SetOfValueComparer.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/SetOfValueComparer.cs b/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/SetOfValueComparer.cs index 648b6a33ab820..44b6c3030957e 100644 --- a/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/SetOfValueComparer.cs +++ b/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/SetOfValueComparer.cs @@ -18,13 +18,9 @@ internal static int Compare(ReadOnlySpan x, ReadOnlySpan y) int diff; #if NET7_0_OR_GREATER - int diffIndex = x.Slice(0, min).CommonPrefixLength(y.Slice(0, min)); + int diffIndex = x.CommonPrefixLength(y); - if (diffIndex == min) - { - diff = 0; - } - else + if (diffIndex != min) { return (int)x[diffIndex] - y[diffIndex]; }