Skip to content

Commit 6960097

Browse files
authored
Enable nullable on DataProtection (#28583)
1 parent 2121105 commit 6960097

File tree

73 files changed

+374
-344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+374
-344
lines changed

eng/targets/CSharp.Common.targets

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@
7777
by compiling the annotations (as internal types).
7878
The code block that follows sets it up so projects in this repo that target ns2.0 or netfx can compile when Nullable is configured.
7979
Based on https://github.com/dotnet/runtime/blob/93b6c449d4f31ddd7d573d1d3769e681d5ebceb9/src/libraries/Directory.Build.targets#L215-L222
80+
81+
SuppressNullableAttributesImport to disable. Used when attributes are already present from another project because of InternalsVisibleTo.
8082
-->
81-
<When Condition="'$(Nullable)' != '' AND ('$(TargetFrameworkIdentifier)' == '.NETStandard' OR '$(TargetFrameworkIdentifier)' == '.NETFramework')">
83+
<When Condition="'$(Nullable)' != '' AND '$(SuppressNullableAttributesImport)' != 'true' AND ('$(TargetFrameworkIdentifier)' == '.NETStandard' OR '$(TargetFrameworkIdentifier)' == '.NETFramework')">
8284
<PropertyGroup>
8385
<DefineConstants>$(DefineConstants),INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
8486
<NoWarn>$(NoWarn);nullable</NoWarn>

src/DataProtection/Abstractions/src/Infrastructure/IApplicationDiscriminator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public interface IApplicationDiscriminator
2020
/// An identifier that uniquely discriminates this application from all other
2121
/// applications on the machine.
2222
/// </summary>
23-
string Discriminator { get; }
23+
string? Discriminator { get; }
2424
}
2525
}

src/DataProtection/Abstractions/src/PublicAPI.Shipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Microsoft.AspNetCore.DataProtection.IDataProtector
66
Microsoft.AspNetCore.DataProtection.IDataProtector.Protect(byte[]! plaintext) -> byte[]!
77
Microsoft.AspNetCore.DataProtection.IDataProtector.Unprotect(byte[]! protectedData) -> byte[]!
88
Microsoft.AspNetCore.DataProtection.Infrastructure.IApplicationDiscriminator
9-
Microsoft.AspNetCore.DataProtection.Infrastructure.IApplicationDiscriminator.Discriminator.get -> string!
9+
Microsoft.AspNetCore.DataProtection.Infrastructure.IApplicationDiscriminator.Discriminator.get -> string?
1010
static Microsoft.AspNetCore.DataProtection.DataProtectionCommonExtensions.CreateProtector(this Microsoft.AspNetCore.DataProtection.IDataProtectionProvider! provider, System.Collections.Generic.IEnumerable<string!>! purposes) -> Microsoft.AspNetCore.DataProtection.IDataProtector!
1111
static Microsoft.AspNetCore.DataProtection.DataProtectionCommonExtensions.CreateProtector(this Microsoft.AspNetCore.DataProtection.IDataProtectionProvider! provider, string! purpose, params string![]! subPurposes) -> Microsoft.AspNetCore.DataProtection.IDataProtector!
1212
static Microsoft.AspNetCore.DataProtection.DataProtectionCommonExtensions.GetDataProtectionProvider(this System.IServiceProvider! services) -> Microsoft.AspNetCore.DataProtection.IDataProtectionProvider!

src/DataProtection/Cryptography.Internal/src/Cng/CachedAlgorithmHandles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private static BCryptAlgorithmHandle GetSP800_108_CTR_HMACAlgorithm()
7575
// Warning: mutable struct!
7676
private struct CachedAlgorithmInfo
7777
{
78-
private WeakReference<BCryptAlgorithmHandle> _algorithmHandle;
78+
private WeakReference<BCryptAlgorithmHandle>? _algorithmHandle;
7979
private readonly Func<BCryptAlgorithmHandle> _factory;
8080

8181
public CachedAlgorithmInfo(Func<BCryptAlgorithmHandle> factory)

src/DataProtection/Cryptography.Internal/src/Cng/OSVersionUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static class OSVersionUtil
1313
private static OSVersion GetOSVersion()
1414
{
1515
const string BCRYPT_LIB = "bcrypt.dll";
16-
SafeLibraryHandle bcryptLibHandle = null;
16+
SafeLibraryHandle? bcryptLibHandle = null;
1717
try
1818
{
1919
bcryptLibHandle = SafeLibraryHandle.Open(BCRYPT_LIB);

src/DataProtection/Cryptography.Internal/src/Microsoft.AspNetCore.Cryptography.Internal.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<PackageTags>aspnetcore;dataprotection</PackageTags>
10-
<Nullable>annotations</Nullable>
10+
<Nullable>enable</Nullable>
1111
</PropertyGroup>
1212

1313
</Project>

src/DataProtection/Cryptography.Internal/src/SafeHandles/BCryptAlgorithmHandle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public uint GetHashDigestLength()
130130
return digestLength;
131131
}
132132

133-
public static BCryptAlgorithmHandle OpenAlgorithmHandle(string algorithmId, string implementation = null, bool hmac = false)
133+
public static BCryptAlgorithmHandle OpenAlgorithmHandle(string algorithmId, string? implementation = null, bool hmac = false)
134134
{
135135
// from bcrypt.h
136136
const uint BCRYPT_ALG_HANDLE_HMAC_FLAG = 0x00000008;
@@ -161,7 +161,7 @@ protected override bool ReleaseHandle()
161161

162162
public void SetChainingMode(string chainingMode)
163163
{
164-
fixed (char* pszChainingMode = chainingMode ?? String.Empty)
164+
fixed (char* pszChainingMode = chainingMode)
165165
{
166166
SetProperty(Constants.BCRYPT_CHAINING_MODE, pszChainingMode, checked((uint)(chainingMode.Length + 1 /* null terminator */) * sizeof(char)));
167167
}

src/DataProtection/Cryptography.Internal/src/SafeHandles/BCryptHashHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Cryptography.SafeHandles
88
{
99
internal unsafe sealed class BCryptHashHandle : BCryptHandle
1010
{
11-
private BCryptAlgorithmHandle _algProviderHandle;
11+
private BCryptAlgorithmHandle? _algProviderHandle;
1212

1313
// Called by P/Invoke when returning SafeHandles
1414
private BCryptHashHandle() { }

src/DataProtection/Cryptography.Internal/src/SafeHandles/BCryptKeyHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.Cryptography.SafeHandles
77
{
88
internal sealed class BCryptKeyHandle : BCryptHandle
99
{
10-
private BCryptAlgorithmHandle _algProviderHandle;
10+
private BCryptAlgorithmHandle? _algProviderHandle;
1111

1212
// Called by P/Invoke when returning SafeHandles
1313
private BCryptKeyHandle() { }

src/DataProtection/Cryptography.Internal/src/SafeHandles/SafeLibraryHandle.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.ConstrainedExecution;
67
using System.Runtime.InteropServices;
78
using System.Security;
@@ -49,7 +50,7 @@ public void ForbidUnload()
4950
/// <summary>
5051
/// Formats a message string using the resource table in the specified library.
5152
/// </summary>
52-
public string FormatMessage(int messageId)
53+
public string? FormatMessage(int messageId)
5354
{
5455
// from winbase.h
5556
const uint FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
@@ -72,7 +73,7 @@ public string FormatMessage(int messageId)
7273
// Successfully retrieved the message.
7374
using (messageHandle)
7475
{
75-
return new String((char*)messageHandle.DangerousGetHandle(), 0, numCharsOutput).Trim();
76+
return new string((char*)messageHandle.DangerousGetHandle(), 0, numCharsOutput).Trim();
7677
}
7778
}
7879
else
@@ -85,7 +86,7 @@ public string FormatMessage(int messageId)
8586
/// <summary>
8687
/// Gets a delegate pointing to a given export from this library.
8788
/// </summary>
88-
public TDelegate GetProcAddress<TDelegate>(string lpProcName, bool throwIfNotFound = true) where TDelegate : class
89+
public TDelegate? GetProcAddress<TDelegate>(string lpProcName, bool throwIfNotFound = true) where TDelegate : class
8990
{
9091
IntPtr pfnProc = UnsafeNativeMethods.GetProcAddress(this, lpProcName);
9192
if (pfnProc == IntPtr.Zero)
@@ -168,11 +169,14 @@ internal static extern SafeLibraryHandle LoadLibraryEx(
168169
[In] IntPtr hFile,
169170
[In] uint dwFlags);
170171

172+
#pragma warning disable CS8763 // A method marked [DoesNotReturn] should not return.
173+
[DoesNotReturn]
171174
internal static void ThrowExceptionForLastWin32Error()
172175
{
173176
int hr = Marshal.GetHRForLastWin32Error();
174177
Marshal.ThrowExceptionForHR(hr);
175178
}
179+
#pragma warning restore CS8763 // A method marked [DoesNotReturn] should not return.
176180
}
177181
}
178182
}

0 commit comments

Comments
 (0)