diff --git a/src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoExW.cs b/src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoExW.cs index c0a48996328ed..430bd89e22fa1 100644 --- a/src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoExW.cs +++ b/src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoExW.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Net.Sockets; using System.Runtime.InteropServices; @@ -20,7 +21,7 @@ internal static partial class Winsock [DllImport(Libraries.Ws2_32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] internal static extern unsafe int GetAddrInfoExW( [In] string pName, - [In] string pServiceName, + [In] string? pServiceName, [In] int dwNamespace, [In] IntPtr lpNspId, [In] AddressInfoEx* pHints, diff --git a/src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoW.cs b/src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoW.cs index 54fb797d22666..06eded1f265b6 100644 --- a/src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoW.cs +++ b/src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoW.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System; using System.Net.Sockets; using System.Runtime.InteropServices; @@ -13,7 +14,7 @@ internal static partial class Winsock [DllImport(Interop.Libraries.Ws2_32, ExactSpelling = true, CharSet = CharSet.Unicode, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)] internal static extern unsafe int GetAddrInfoW( [In] string pNameName, - [In] string pServiceName, + [In] string? pServiceName, [In] AddressInfo* pHints, [Out] AddressInfo** ppResult); diff --git a/src/libraries/Common/src/System/Net/DebugSafeHandle.cs b/src/libraries/Common/src/System/Net/DebugSafeHandle.cs index ad41a64da514b..8cbe492a886c2 100644 --- a/src/libraries/Common/src/System/Net/DebugSafeHandle.cs +++ b/src/libraries/Common/src/System/Net/DebugSafeHandle.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using Microsoft.Win32.SafeHandles; namespace System.Net @@ -13,7 +14,7 @@ namespace System.Net // internal abstract class DebugSafeHandle : SafeHandleZeroOrMinusOneIsInvalid { - private string _trace; + private string? _trace; protected DebugSafeHandle(bool ownsHandle) : base(ownsHandle) { diff --git a/src/libraries/Common/src/System/Net/InternalException.cs b/src/libraries/Common/src/System/Net/InternalException.cs index 271bb47a1afdb..676da377c9a7f 100644 --- a/src/libraries/Common/src/System/Net/InternalException.cs +++ b/src/libraries/Common/src/System/Net/InternalException.cs @@ -2,11 +2,12 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable namespace System.Net { internal sealed class InternalException : Exception { - private readonly object _unexpectedValue; + private readonly object? _unexpectedValue; internal InternalException() { diff --git a/src/libraries/Common/src/System/Net/Internals/SocketExceptionFactory.cs b/src/libraries/Common/src/System/Net/Internals/SocketExceptionFactory.cs index 3f2396b52f450..217ff1a49dd8e 100644 --- a/src/libraries/Common/src/System/Net/Internals/SocketExceptionFactory.cs +++ b/src/libraries/Common/src/System/Net/Internals/SocketExceptionFactory.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Net.Sockets; namespace System.Net.Internals @@ -10,7 +11,7 @@ internal static partial class SocketExceptionFactory { private sealed class ExtendedSocketException : SocketException { - private readonly EndPoint _endPoint; + private readonly EndPoint? _endPoint; public ExtendedSocketException(int errorCode, EndPoint endPoint) : base(errorCode) diff --git a/src/libraries/Common/src/System/Net/Logging/DebugThreadTracking.cs b/src/libraries/Common/src/System/Net/Logging/DebugThreadTracking.cs index c2b3e03474ef3..eba97da8e3b20 100644 --- a/src/libraries/Common/src/System/Net/Logging/DebugThreadTracking.cs +++ b/src/libraries/Common/src/System/Net/Logging/DebugThreadTracking.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Collections.Generic; namespace System.Net @@ -9,13 +10,13 @@ namespace System.Net internal static class DebugThreadTracking { [ThreadStatic] - private static Stack t_threadKindStack; + private static Stack? t_threadKindStack; private static Stack ThreadKindStack => t_threadKindStack ?? (t_threadKindStack = new Stack()); internal static ThreadKinds CurrentThreadKind => ThreadKindStack.Count > 0 ? ThreadKindStack.Peek() : ThreadKinds.Other; - internal static IDisposable SetThreadKind(ThreadKinds kind) + internal static IDisposable? SetThreadKind(ThreadKinds kind) { if ((kind & ThreadKinds.SourceMask) != ThreadKinds.Unknown) { diff --git a/src/libraries/Common/src/System/Net/Logging/NetEventSource.Common.cs b/src/libraries/Common/src/System/Net/Logging/NetEventSource.Common.cs index 191cdb834df64..46cd2ee685c8f 100644 --- a/src/libraries/Common/src/System/Net/Logging/NetEventSource.Common.cs +++ b/src/libraries/Common/src/System/Net/Logging/NetEventSource.Common.cs @@ -100,7 +100,7 @@ public class Keywords /// A description of the entrance, including any arguments to the call. /// The calling member. [NonEvent] - public static void Enter(object thisOrContextObject, FormattableString? formattableString = null, [CallerMemberName] string? memberName = null) + public static void Enter(object? thisOrContextObject, FormattableString? formattableString = null, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(formattableString); @@ -112,7 +112,7 @@ public static void Enter(object thisOrContextObject, FormattableString? formatta /// The object to log. /// The calling member. [NonEvent] - public static void Enter(object thisOrContextObject, object arg0, [CallerMemberName] string? memberName = null) + public static void Enter(object? thisOrContextObject, object arg0, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(arg0); @@ -125,7 +125,7 @@ public static void Enter(object thisOrContextObject, object arg0, [CallerMemberN /// The second object to log. /// The calling member. [NonEvent] - public static void Enter(object thisOrContextObject, object arg0, object arg1, [CallerMemberName] string? memberName = null) + public static void Enter(object? thisOrContextObject, object arg0, object arg1, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(arg0); @@ -140,7 +140,7 @@ public static void Enter(object thisOrContextObject, object arg0, object arg1, [ /// The third object to log. /// The calling member. [NonEvent] - public static void Enter(object thisOrContextObject, object arg0, object arg1, object arg2, [CallerMemberName] string? memberName = null) + public static void Enter(object? thisOrContextObject, object arg0, object arg1, object arg2, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(arg0); @@ -160,7 +160,7 @@ private void Enter(string thisOrContextObject, string? memberName, string parame /// A description of the exit operation, including any return values. /// The calling member. [NonEvent] - public static void Exit(object thisOrContextObject, FormattableString? formattableString = null, [CallerMemberName] string? memberName = null) + public static void Exit(object? thisOrContextObject, FormattableString? formattableString = null, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(formattableString); @@ -172,7 +172,7 @@ public static void Exit(object thisOrContextObject, FormattableString? formattab /// A return value from the member. /// The calling member. [NonEvent] - public static void Exit(object thisOrContextObject, object arg0, [CallerMemberName] string? memberName = null) + public static void Exit(object? thisOrContextObject, object arg0, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(arg0); @@ -185,7 +185,7 @@ public static void Exit(object thisOrContextObject, object arg0, [CallerMemberNa /// A second return value from the member. /// The calling member. [NonEvent] - public static void Exit(object thisOrContextObject, object arg0, object arg1, [CallerMemberName] string? memberName = null) + public static void Exit(object? thisOrContextObject, object arg0, object arg1, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(arg0); @@ -204,7 +204,7 @@ private void Exit(string thisOrContextObject, string? memberName, string? result /// The message to be logged. /// The calling member. [NonEvent] - public static void Info(object thisOrContextObject, FormattableString? formattableString = null, [CallerMemberName] string? memberName = null) + public static void Info(object? thisOrContextObject, FormattableString? formattableString = null, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(formattableString); @@ -216,7 +216,7 @@ public static void Info(object thisOrContextObject, FormattableString? formattab /// The message to be logged. /// The calling member. [NonEvent] - public static void Info(object thisOrContextObject, object message, [CallerMemberName] string? memberName = null) + public static void Info(object? thisOrContextObject, object? message, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(message); @@ -234,7 +234,7 @@ private void Info(string thisOrContextObject, string? memberName, string? messag /// The message to be logged. /// The calling member. [NonEvent] - public static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string? memberName = null) + public static void Error(object? thisOrContextObject, FormattableString formattableString, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(formattableString); @@ -246,7 +246,7 @@ public static void Error(object thisOrContextObject, FormattableString formattab /// The message to be logged. /// The calling member. [NonEvent] - public static void Error(object thisOrContextObject, object message, [CallerMemberName] string? memberName = null) + public static void Error(object? thisOrContextObject, object message, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(message); @@ -264,7 +264,7 @@ private void ErrorMessage(string thisOrContextObject, string? memberName, string /// The message to be logged. /// The calling member. [NonEvent] - public static void Fail(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string? memberName = null) + public static void Fail(object? thisOrContextObject, FormattableString formattableString, [CallerMemberName] string? memberName = null) { // Don't call DebugValidateArg on args, as we expect Fail to be used in assert/failure situations // that should never happen in production, and thus we don't care about extra costs. @@ -278,7 +278,7 @@ public static void Fail(object thisOrContextObject, FormattableString formattabl /// The message to be logged. /// The calling member. [NonEvent] - public static void Fail(object thisOrContextObject, object message, [CallerMemberName] string? memberName = null) + public static void Fail(object? thisOrContextObject, object message, [CallerMemberName] string? memberName = null) { // Don't call DebugValidateArg on args, as we expect Fail to be used in assert/failure situations // that should never happen in production, and thus we don't care about extra costs. @@ -298,7 +298,7 @@ private void CriticalFailure(string thisOrContextObject, string? memberName, str /// The buffer to be logged. /// The calling member. [NonEvent] - public static void DumpBuffer(object thisOrContextObject, byte[] buffer, [CallerMemberName] string? memberName = null) + public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, [CallerMemberName] string? memberName = null) { DumpBuffer(thisOrContextObject, buffer, 0, buffer.Length, memberName); } @@ -310,7 +310,7 @@ public static void DumpBuffer(object thisOrContextObject, byte[] buffer, [Caller /// The number of bytes to log. /// The calling member. [NonEvent] - public static void DumpBuffer(object thisOrContextObject, byte[] buffer, int offset, int count, [CallerMemberName] string? memberName = null) + public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, int offset, int count, [CallerMemberName] string? memberName = null) { if (IsEnabled) { @@ -339,7 +339,7 @@ public static void DumpBuffer(object thisOrContextObject, byte[] buffer, int off /// The number of bytes to log. /// The calling member. [NonEvent] - public static unsafe void DumpBuffer(object thisOrContextObject, IntPtr bufferPtr, int count, [CallerMemberName] string? memberName = null) + public static unsafe void DumpBuffer(object? thisOrContextObject, IntPtr bufferPtr, int count, [CallerMemberName] string? memberName = null) { Debug.Assert(bufferPtr != IntPtr.Zero); Debug.Assert(count >= 0); @@ -379,7 +379,7 @@ public static void Associate(object first, object second, [CallerMemberName] str /// The second object. /// The calling member. [NonEvent] - public static void Associate(object thisOrContextObject, object first, object second, [CallerMemberName] string? memberName = null) + public static void Associate(object? thisOrContextObject, object first, object second, [CallerMemberName] string? memberName = null) { DebugValidateArg(thisOrContextObject); DebugValidateArg(first); @@ -395,7 +395,7 @@ private void Associate(string thisOrContextObject, string? memberName, string fi #region Helpers [Conditional("DEBUG_NETEVENTSOURCE_MISUSE")] - private static void DebugValidateArg(object arg) + private static void DebugValidateArg(object? arg) { if (!IsEnabled) { @@ -414,7 +414,7 @@ private static void DebugValidateArg(FormattableString? arg) Log.IsEnabled(); [NonEvent] - public static string IdOf(object value) => value != null ? value.GetType().Name + "#" + GetHashCode(value) : NullInstance; + public static string IdOf(object? value) => value != null ? value.GetType().Name + "#" + GetHashCode(value) : NullInstance; [NonEvent] public static int GetHashCode(object value) => value?.GetHashCode() ?? 0; @@ -498,7 +498,7 @@ private static string Format(FormattableString s) #region Custom WriteEvent overloads [NonEvent] - private unsafe void WriteEvent(int eventId, string arg1, string arg2, string arg3, string arg4) + private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string? arg3, string? arg4) { if (IsEnabled()) { @@ -542,7 +542,7 @@ private unsafe void WriteEvent(int eventId, string arg1, string arg2, string arg } [NonEvent] - private unsafe void WriteEvent(int eventId, string arg1, string arg2, byte[] arg3) + private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, byte[]? arg3) { if (IsEnabled()) { @@ -585,7 +585,7 @@ private unsafe void WriteEvent(int eventId, string arg1, string arg2, byte[] arg } [NonEvent] - private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int arg4) + private unsafe void WriteEvent(int eventId, string? arg1, int arg2, int arg3, int arg4) { if (IsEnabled()) { @@ -623,7 +623,7 @@ private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int } [NonEvent] - private unsafe void WriteEvent(int eventId, string arg1, int arg2, string arg3) + private unsafe void WriteEvent(int eventId, string? arg1, int arg2, string? arg3) { if (IsEnabled()) { @@ -658,7 +658,7 @@ private unsafe void WriteEvent(int eventId, string arg1, int arg2, string arg3) } [NonEvent] - private unsafe void WriteEvent(int eventId, string arg1, string arg2, int arg3) + private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, int arg3) { if (IsEnabled()) { @@ -693,7 +693,7 @@ private unsafe void WriteEvent(int eventId, string arg1, string arg2, int arg3) } [NonEvent] - private unsafe void WriteEvent(int eventId, string arg1, string arg2, string arg3, int arg4) + private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string? arg3, int arg4) { if (IsEnabled()) { diff --git a/src/libraries/System.Net.NameResolution/ref/System.Net.NameResolution.cs b/src/libraries/System.Net.NameResolution/ref/System.Net.NameResolution.cs index 877bfa9ea94de..2e338b2847bc1 100644 --- a/src/libraries/System.Net.NameResolution/ref/System.Net.NameResolution.cs +++ b/src/libraries/System.Net.NameResolution/ref/System.Net.NameResolution.cs @@ -9,13 +9,13 @@ namespace System.Net { public static partial class Dns { - public static System.IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, System.AsyncCallback requestCallback, object state) { throw null; } + public static System.IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, System.AsyncCallback? requestCallback, object? state) { throw null; } [System.ObsoleteAttribute("BeginGetHostByName is obsoleted for this type, please use BeginGetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")] - public static System.IAsyncResult BeginGetHostByName(string hostName, System.AsyncCallback requestCallback, object stateObject) { throw null; } - public static System.IAsyncResult BeginGetHostEntry(System.Net.IPAddress address, System.AsyncCallback requestCallback, object stateObject) { throw null; } - public static System.IAsyncResult BeginGetHostEntry(string hostNameOrAddress, System.AsyncCallback requestCallback, object stateObject) { throw null; } + public static System.IAsyncResult BeginGetHostByName(string hostName, System.AsyncCallback? requestCallback, object? stateObject) { throw null; } + public static System.IAsyncResult BeginGetHostEntry(System.Net.IPAddress address, System.AsyncCallback? requestCallback, object? stateObject) { throw null; } + public static System.IAsyncResult BeginGetHostEntry(string hostNameOrAddress, System.AsyncCallback? requestCallback, object? stateObject) { throw null; } [System.ObsoleteAttribute("BeginResolve is obsoleted for this type, please use BeginGetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")] - public static System.IAsyncResult BeginResolve(string hostName, System.AsyncCallback requestCallback, object stateObject) { throw null; } + public static System.IAsyncResult BeginResolve(string hostName, System.AsyncCallback? requestCallback, object? stateObject) { throw null; } public static System.Net.IPAddress[] EndGetHostAddresses(System.IAsyncResult asyncResult) { throw null; } [System.ObsoleteAttribute("EndGetHostByName is obsoleted for this type, please use EndGetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")] public static System.Net.IPHostEntry EndGetHostByName(System.IAsyncResult asyncResult) { throw null; } diff --git a/src/libraries/System.Net.NameResolution/ref/System.Net.NameResolution.csproj b/src/libraries/System.Net.NameResolution/ref/System.Net.NameResolution.csproj index d2d64fe5f0fa3..8c301a790a442 100644 --- a/src/libraries/System.Net.NameResolution/ref/System.Net.NameResolution.csproj +++ b/src/libraries/System.Net.NameResolution/ref/System.Net.NameResolution.csproj @@ -1,6 +1,7 @@ $(NetCoreAppCurrent) + enable diff --git a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj index 0af1d2d62afe6..e4107a9b1a329 100644 --- a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj +++ b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj @@ -3,6 +3,7 @@ System.Net.NameResolution true $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix + enable diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs b/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs index 63f74b7722b1b..512f195155384 100644 --- a/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs +++ b/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs @@ -82,7 +82,7 @@ public static Task GetHostEntryAsync(string hostNameOrAddress) { NetEventSource.Enter(hostNameOrAddress, hostNameOrAddress); Task t = GetHostEntryCoreAsync(hostNameOrAddress, justReturnParsedIp: false, throwOnIIPAny: true); - t.ContinueWith((t, s) => NetEventSource.Exit((string)s, $"{t.Result} with {((IPHostEntry)t.Result).AddressList.Length} entries"), + t.ContinueWith((t, s) => NetEventSource.Exit((string)s!, $"{t.Result} with {((IPHostEntry)t.Result).AddressList.Length} entries"), hostNameOrAddress, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default); return t; } @@ -116,7 +116,7 @@ public static Task GetHostEntryAsync(IPAddress address) }, address); } - public static IAsyncResult BeginGetHostEntry(IPAddress address, AsyncCallback requestCallback, object stateObject) + public static IAsyncResult BeginGetHostEntry(IPAddress address, AsyncCallback? requestCallback, object? stateObject) { if (NetEventSource.IsEnabled) NetEventSource.Enter(address, address); @@ -126,7 +126,7 @@ public static IAsyncResult BeginGetHostEntry(IPAddress address, AsyncCallback re return asyncResult; } - public static IAsyncResult BeginGetHostEntry(string hostNameOrAddress, AsyncCallback requestCallback, object stateObject) + public static IAsyncResult BeginGetHostEntry(string hostNameOrAddress, AsyncCallback? requestCallback, object? stateObject) { if (NetEventSource.IsEnabled) NetEventSource.Enter(hostNameOrAddress, hostNameOrAddress); @@ -180,7 +180,7 @@ public static IPAddress[] GetHostAddresses(string hostNameOrAddress) public static Task GetHostAddressesAsync(string hostNameOrAddress) => (Task)GetHostEntryOrAddressesCoreAsync(hostNameOrAddress, justReturnParsedIp: true, throwOnIIPAny: true, justAddresses: true); - public static IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, AsyncCallback requestCallback, object state) + public static IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, AsyncCallback? requestCallback, object? state) { if (NetEventSource.IsEnabled) NetEventSource.Enter(hostNameOrAddress, hostNameOrAddress); @@ -219,7 +219,7 @@ public static IPHostEntry GetHostByName(string hostName) } [Obsolete("BeginGetHostByName is obsoleted for this type, please use BeginGetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")] - public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback requestCallback, object stateObject) + public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback? requestCallback, object? stateObject) { if (NetEventSource.IsEnabled) NetEventSource.Enter(hostName, hostName); @@ -310,7 +310,7 @@ public static IPHostEntry Resolve(string hostName) } [Obsolete("BeginResolve is obsoleted for this type, please use BeginGetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")] - public static IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject) + public static IAsyncResult BeginResolve(string hostName, AsyncCallback? requestCallback, object? stateObject) { if (NetEventSource.IsEnabled) NetEventSource.Enter(null, hostName); @@ -332,7 +332,7 @@ public static IPHostEntry EndResolve(IAsyncResult asyncResult) } catch (SocketException ex) { - IPAddress address = asyncResult switch + IPAddress? address = asyncResult switch { Task t => t.AsyncState as IPAddress, TaskToApm.TaskAsyncResult twar => twar._task.AsyncState as IPAddress, @@ -360,7 +360,7 @@ private static object GetHostEntryOrAddressesCore(string hostName, bool justAddr { ValidateHostName(hostName); - SocketError errorCode = NameResolutionPal.TryGetAddrInfo(hostName, justAddresses, out string newHostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode); + SocketError errorCode = NameResolutionPal.TryGetAddrInfo(hostName, justAddresses, out string? newHostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode); if (errorCode != SocketError.Success) { @@ -373,7 +373,7 @@ private static object GetHostEntryOrAddressesCore(string hostName, bool justAddr new IPHostEntry { AddressList = addresses, - HostName = newHostName, + HostName = newHostName!, Aliases = aliases }; @@ -394,7 +394,7 @@ private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAd // will only return that address and not the full list. // Do a reverse lookup to get the host name. - string name = NameResolutionPal.TryGetNameInfo(address, out SocketError errorCode, out int nativeErrorCode); + string? name = NameResolutionPal.TryGetNameInfo(address, out SocketError errorCode, out int nativeErrorCode); if (errorCode != SocketError.Success) { if (NetEventSource.IsEnabled) NetEventSource.Error(address, $"{address} DNS lookup failed with {errorCode}"); @@ -402,7 +402,7 @@ private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAd } // Do the forward lookup to get the IPs for that host name - errorCode = NameResolutionPal.TryGetAddrInfo(name, justAddresses, out string hostName, out string[] aliases, out IPAddress[] addresses, out nativeErrorCode); + errorCode = NameResolutionPal.TryGetAddrInfo(name!, justAddresses, out string? hostName, out string[] aliases, out IPAddress[] addresses, out nativeErrorCode); if (errorCode != SocketError.Success) { @@ -421,7 +421,7 @@ private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAd (object)addresses : new IPHostEntry { - HostName = hostName, + HostName = hostName!, Aliases = aliases, AddressList = addresses }; @@ -475,7 +475,7 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR } private static Task RunAsync(Func func, object arg) => - Task.Factory.StartNew(func, arg, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + Task.Factory.StartNew(func!, arg, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); private static IPHostEntry CreateHostEntryForAddress(IPAddress address) => new IPHostEntry diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/IPHostEntry.cs b/src/libraries/System.Net.NameResolution/src/System/Net/IPHostEntry.cs index e9029df764ff2..757b207fcc824 100644 --- a/src/libraries/System.Net.NameResolution/src/System/Net/IPHostEntry.cs +++ b/src/libraries/System.Net.NameResolution/src/System/Net/IPHostEntry.cs @@ -7,6 +7,14 @@ namespace System.Net /// Provides a container class for Internet host address information. public class IPHostEntry { + // Technically there's nothing to prevent someone from doing `new IPHostEntry()`, at which point + // all of these fields will be null. However, that it not the intended usage of this type, which + // is intended only to be returned from the various methods on the Dns type (and things that wrap + // it), in which case the implementation will set all of the members to be non-null. Thus, the + // intent is that these be non-nullable. Ideally the type would have been designed originally with + // its ctor being internal-only. + #pragma warning disable CS8618 + /// Gets or sets the DNS name of the host. public string HostName { get; set; } diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs index ff923551b8316..d76240e706b10 100644 --- a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs +++ b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs @@ -46,7 +46,7 @@ private static SocketError GetSocketErrorForNativeError(int error) } } - private static unsafe void ParseHostEntry(Interop.Sys.HostEntry hostEntry, bool justAddresses, out string hostName, out string[] aliases, out IPAddress[] addresses) + private static unsafe void ParseHostEntry(Interop.Sys.HostEntry hostEntry, bool justAddresses, out string? hostName, out string[] aliases, out IPAddress[] addresses) { try { @@ -105,7 +105,7 @@ private static unsafe void ParseHostEntry(Interop.Sys.HostEntry hostEntry, bool localAliases = new string[numAliases]; for (int i = 0; i < localAliases.Length; i++) { - localAliases[i] = Marshal.PtrToStringAnsi((IntPtr)hostEntry.Aliases[i]); + localAliases[i] = Marshal.PtrToStringAnsi((IntPtr)hostEntry.Aliases[i])!; } } } @@ -119,7 +119,7 @@ private static unsafe void ParseHostEntry(Interop.Sys.HostEntry hostEntry, bool } } - public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses, out string hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode) + public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses, out string? hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode) { if (name == "") { @@ -143,7 +143,7 @@ public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses, return SocketError.Success; } - public static unsafe string TryGetNameInfo(IPAddress addr, out SocketError socketError, out int nativeErrorCode) + public static unsafe string? TryGetNameInfo(IPAddress addr, out SocketError socketError, out int nativeErrorCode) { byte* buffer = stackalloc byte[Interop.Sys.NI_MAXHOST + 1 /*for null*/]; diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs index 3ba5c097d7bf1..8fe45abcc700e 100644 --- a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs +++ b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs @@ -56,7 +56,7 @@ public static bool SupportsGetAddrInfoAsync } } - public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses, out string hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode) + public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses, out string? hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode) { aliases = Array.Empty(); @@ -91,7 +91,7 @@ public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses, } } - public static unsafe string TryGetNameInfo(IPAddress addr, out SocketError errorCode, out int nativeErrorCode) + public static unsafe string? TryGetNameInfo(IPAddress addr, out SocketError errorCode, out int nativeErrorCode) { SocketAddress address = new IPEndPoint(addr, 0).Serialize(); Span addressBuffer = address.Size <= 64 ? stackalloc byte[64] : new byte[address.Size]; @@ -191,7 +191,7 @@ private static unsafe void ProcessResult(SocketError errorCode, GetAddrInfoExCon if (errorCode == SocketError.Success) { - IPAddress[] addresses = ParseAddressInfoEx(context->Result, state.JustAddresses, out string hostName); + IPAddress[] addresses = ParseAddressInfoEx(context->Result, state.JustAddresses, out string? hostName); state.SetResult(state.JustAddresses ? (object) addresses : new IPHostEntry @@ -212,7 +212,7 @@ private static unsafe void ProcessResult(SocketError errorCode, GetAddrInfoExCon } } - private static unsafe IPAddress[] ParseAddressInfo(Interop.Winsock.AddressInfo* addressInfoPtr, bool justAddresses, out string hostName) + private static unsafe IPAddress[] ParseAddressInfo(Interop.Winsock.AddressInfo* addressInfoPtr, bool justAddresses, out string? hostName) { Debug.Assert(addressInfoPtr != null); @@ -241,7 +241,7 @@ private static unsafe IPAddress[] ParseAddressInfo(Interop.Winsock.AddressInfo* // Store them into the array. var addresses = new IPAddress[addressCount]; addressCount = 0; - string canonicalName = justAddresses ? "NONNULLSENTINEL" : null; + string? canonicalName = justAddresses ? "NONNULLSENTINEL" : null; for (Interop.Winsock.AddressInfo* result = addressInfoPtr; result != null; result = result->ai_next) { if (canonicalName == null && result->ai_canonname != null) @@ -272,7 +272,7 @@ private static unsafe IPAddress[] ParseAddressInfo(Interop.Winsock.AddressInfo* return addresses; } - private static unsafe IPAddress[] ParseAddressInfoEx(Interop.Winsock.AddressInfoEx* addressInfoExPtr, bool justAddresses, out string hostName) + private static unsafe IPAddress[] ParseAddressInfoEx(Interop.Winsock.AddressInfoEx* addressInfoExPtr, bool justAddresses, out string? hostName) { Debug.Assert(addressInfoExPtr != null); @@ -301,7 +301,7 @@ private static unsafe IPAddress[] ParseAddressInfoEx(Interop.Winsock.AddressInfo // Then store them into an array. var addresses = new IPAddress[addressCount]; addressCount = 0; - string canonicalName = justAddresses ? "NONNULLSENTINEL" : null; + string? canonicalName = justAddresses ? "NONNULLSENTINEL" : null; for (Interop.Winsock.AddressInfoEx* result = addressInfoExPtr; result != null; result = result->ai_next) { if (canonicalName == null && result->ai_canonname != IntPtr.Zero) @@ -350,7 +350,7 @@ private sealed class GetAddrInfoExState : IThreadPoolWorkItem { private AsyncTaskMethodBuilder IPHostEntryBuilder; private AsyncTaskMethodBuilder IPAddressArrayBuilder; - private object _result; + private object? _result; public GetAddrInfoExState(string hostName, bool justAddresses) { @@ -396,7 +396,7 @@ void IThreadPoolWorkItem.Execute() } else { - IPAddressArrayBuilder.SetResult((IPAddress[])_result); + IPAddressArrayBuilder.SetResult((IPAddress[])_result!); } } else @@ -407,7 +407,7 @@ void IThreadPoolWorkItem.Execute() } else { - IPHostEntryBuilder.SetResult((IPHostEntry)_result); + IPHostEntryBuilder.SetResult((IPHostEntry)_result!); } } } @@ -417,7 +417,7 @@ void IThreadPoolWorkItem.Execute() public static GetAddrInfoExState FromHandleAndFree(IntPtr handle) { GCHandle gcHandle = GCHandle.FromIntPtr(handle); - var state = (GetAddrInfoExState)gcHandle.Target; + var state = (GetAddrInfoExState)gcHandle.Target!; gcHandle.Free(); return state; } diff --git a/src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj b/src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj index 256d41a19abf2..7fcacc540d25d 100644 --- a/src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj +++ b/src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj @@ -3,6 +3,7 @@ true ../../src/Resources/Strings.resx $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix + annotations diff --git a/src/libraries/System.Net.NameResolution/tests/UnitTests/System.Net.NameResolution.Unit.Tests.csproj b/src/libraries/System.Net.NameResolution/tests/UnitTests/System.Net.NameResolution.Unit.Tests.csproj index 4c16273bf3116..ff5ea142ae7ac 100644 --- a/src/libraries/System.Net.NameResolution/tests/UnitTests/System.Net.NameResolution.Unit.Tests.csproj +++ b/src/libraries/System.Net.NameResolution/tests/UnitTests/System.Net.NameResolution.Unit.Tests.csproj @@ -5,6 +5,7 @@ 0436 $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix + annotations