Skip to content

Commit fcc0209

Browse files
buyaa-nstephentoub
andauthored
Do not compare Span<T> with 'default' (#99095)
* Do not compare Span<T> with 'default' * Update comments Co-authored-by: Stephen Toub <[email protected]> --------- Co-authored-by: Stephen Toub <[email protected]>
1 parent b4d4c3d commit fcc0209

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeUnicodeStringHandle.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Runtime.CompilerServices;
56
using System.Runtime.InteropServices;
67

78
namespace Microsoft.Win32.SafeHandles
@@ -23,12 +24,12 @@ public SafeUnicodeStringHandle(string s)
2324
public unsafe SafeUnicodeStringHandle(ReadOnlySpan<char> s)
2425
: base(IntPtr.Zero, ownsHandle: true)
2526
{
26-
// If s == default then the span represents the null string,
27+
// If s contains a null ref then the span represents the null string,
2728
// and handle should be IntPtr.Zero to match Marshal.StringToHGlobalUni.
2829
//
2930
// Since that was already done in the base ctor call, we only need to do
30-
// work when s != default.
31-
if (s != default)
31+
// work when s does not contain a null ref.
32+
if (!Unsafe.IsNullRef(ref MemoryMarshal.GetReference(s)))
3233
{
3334
int cch = checked(s.Length + 1);
3435
int cb = checked(cch * sizeof(char));

src/libraries/Common/src/System/Security/Cryptography/Pkcs12Kdf.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
68
using System.Text;
79

810
namespace System.Security.Cryptography.Pkcs
@@ -115,9 +117,15 @@ private static void Derive(
115117
// The password is a null-terminated UTF-16BE version of the input.
116118
int passLen = checked((password.Length + 1) * 2);
117119

118-
// If password == default then the span represents the null string (as opposed to
120+
// If password contains a null ref then the span represents the null string (as opposed to
119121
// an empty string), and the P block should then have size 0 in the next step.
122+
#if NETSTANDARD
123+
#pragma warning disable CA2265 // Do not compare Span<T> to 'default'
120124
if (password == default)
125+
#pragma warning restore CA2265
126+
#else
127+
if (Unsafe.IsNullRef(ref MemoryMarshal.GetReference(password)))
128+
#endif
121129
{
122130
passLen = 0;
123131
}

src/libraries/System.Security.Cryptography/src/Microsoft/Win32/SafeHandles/SafePasswordHandle.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Runtime.CompilerServices;
56
using System.Runtime.InteropServices;
67
using System.Security;
78

@@ -35,8 +36,8 @@ public SafePasswordHandle(string? password, bool passwordProvided)
3536
public SafePasswordHandle(ReadOnlySpan<char> password, bool passwordProvided)
3637
: base(ownsHandle: true)
3738
{
38-
// "".AsSpan() is not default, so this is compat for "null tries NULL first".
39-
if (password != default)
39+
// "".AsSpan() does not contain a null ref, so this is compat for "null tries NULL first".
40+
if (!Unsafe.IsNullRef(ref MemoryMarshal.GetReference(password)))
4041
{
4142
int spanLen;
4243

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
77
using System.Runtime.CompilerServices;
8+
using System.Runtime.InteropServices;
89

910
namespace System.Text.Json
1011
{
@@ -1243,7 +1244,7 @@ public bool ValueEquals(ReadOnlySpan<byte> utf8Text)
12431244
if (TokenType == JsonTokenType.Null)
12441245
{
12451246
// This is different than Length == 0, in that it tests true for null, but false for ""
1246-
return utf8Text == default;
1247+
return Unsafe.IsNullRef(ref MemoryMarshal.GetReference(utf8Text));
12471248
}
12481249

12491250
return TextEqualsHelper(utf8Text, isPropertyName: false, shouldUnescape: true);
@@ -1271,7 +1272,7 @@ public bool ValueEquals(ReadOnlySpan<char> text)
12711272
if (TokenType == JsonTokenType.Null)
12721273
{
12731274
// This is different than Length == 0, in that it tests true for null, but false for ""
1274-
return text == default;
1275+
return Unsafe.IsNullRef(ref MemoryMarshal.GetReference(text));
12751276
}
12761277

12771278
return TextEqualsHelper(text, isPropertyName: false);

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCaseEquivalences.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static bool TryFindCaseEquivalencesForCharWithIBehavior(char c, CultureIn
5252
// Default
5353
_ => default
5454
};
55-
return equivalences != default;
55+
return !equivalences.IsEmpty;
5656
}
5757
else
5858
{

0 commit comments

Comments
 (0)