From: Ilya Date: Fri, 21 Feb 2020 17:59:19 +0000 (+0500) Subject: Annotate System.Net.Primitives for nullable (#31794) X-Git-Tag: submit/tizen/20210909.063632~9568 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b59225da897449e498712e763f8e00011f659441;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Annotate System.Net.Primitives for nullable (#31794) * Annotate System.Net.Primitives for nullable * Fix build breaks in other libraries due to new annotations Co-authored-by: Stephen Toub --- diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetDomainName.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetDomainName.cs index 4466d63..a0f8494 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetDomainName.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetDomainName.cs @@ -33,7 +33,7 @@ internal static partial class Interop } // Marshal.PtrToStringAnsi uses UTF8 on Unix. - return Marshal.PtrToStringAnsi((IntPtr)name); + return Marshal.PtrToStringAnsi((IntPtr)name)!; } } } diff --git a/src/libraries/Common/src/System/Net/CookieParser.cs b/src/libraries/Common/src/System/Net/CookieParser.cs index d602ffe..c16c3fd 100644 --- a/src/libraries/Common/src/System/Net/CookieParser.cs +++ b/src/libraries/Common/src/System/Net/CookieParser.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.Diagnostics; using System.Globalization; using System.Reflection; @@ -45,7 +46,7 @@ namespace System.Net private bool _eofCookie; private int _index; private readonly int _length; - private string _name; + private string? _name; private bool _quoted; private int _start; private CookieToken _token; @@ -81,7 +82,7 @@ namespace System.Net } } - internal string Name + internal string? Name { get { @@ -449,7 +450,7 @@ namespace System.Net } } - internal bool IsEqualTo(string value) + internal bool IsEqualTo(string? value) { return string.Equals(_name, value, StringComparison.OrdinalIgnoreCase); } @@ -510,7 +511,7 @@ namespace System.Net internal struct CookieParser { private CookieTokenizer _tokenizer; - private Cookie _savedCookie; + private Cookie? _savedCookie; internal CookieParser(string cookieString) { @@ -519,13 +520,13 @@ namespace System.Net } #if SYSTEM_NET_PRIMITIVES_DLL - private static bool InternalSetNameMethod(Cookie cookie, string value) + private static bool InternalSetNameMethod(Cookie cookie, string? value) { return cookie.InternalSetName(value); } #else - private static Func s_internalSetNameMethod; - private static Func InternalSetNameMethod + private static Func? s_internalSetNameMethod = null; + private static Func InternalSetNameMethod { get { @@ -535,9 +536,9 @@ namespace System.Net // We need to use Cookie.InternalSetName instead of the Cookie.set_Name wrapped in a try catch block, as // Cookie.set_Name keeps the original name if the string is empty or null. // Unfortunately this API is internal so we use reflection to access it. The method is cached for performance reasons. - MethodInfo method = typeof(Cookie).GetMethod("InternalSetName", BindingFlags.Instance | BindingFlags.NonPublic); + MethodInfo? method = typeof(Cookie).GetMethod("InternalSetName", BindingFlags.Instance | BindingFlags.NonPublic); Debug.Assert(method != null, "We need to use an internal method named InternalSetName that is declared on Cookie."); - s_internalSetNameMethod = (Func)Delegate.CreateDelegate(typeof(Func), method); + s_internalSetNameMethod = (Func)Delegate.CreateDelegate(typeof(Func), method); } return s_internalSetNameMethod; @@ -545,7 +546,7 @@ namespace System.Net } #endif - private static FieldInfo s_isQuotedDomainField = null; + private static FieldInfo? s_isQuotedDomainField = null; private static FieldInfo IsQuotedDomainField { get @@ -553,7 +554,7 @@ namespace System.Net if (s_isQuotedDomainField == null) { // TODO https://github.com/dotnet/runtime/issues/19348: - FieldInfo field = typeof(Cookie).GetField("IsQuotedDomain", BindingFlags.Instance | BindingFlags.NonPublic); + FieldInfo? field = typeof(Cookie).GetField("IsQuotedDomain", BindingFlags.Instance | BindingFlags.NonPublic); Debug.Assert(field != null, "We need to use an internal field named IsQuotedDomain that is declared on Cookie."); s_isQuotedDomainField = field; } @@ -562,7 +563,7 @@ namespace System.Net } } - private static FieldInfo s_isQuotedVersionField = null; + private static FieldInfo? s_isQuotedVersionField; private static FieldInfo IsQuotedVersionField { get @@ -570,7 +571,7 @@ namespace System.Net if (s_isQuotedVersionField == null) { // TODO https://github.com/dotnet/runtime/issues/19348: - FieldInfo field = typeof(Cookie).GetField("IsQuotedVersion", BindingFlags.Instance | BindingFlags.NonPublic); + FieldInfo? field = typeof(Cookie).GetField("IsQuotedVersion", BindingFlags.Instance | BindingFlags.NonPublic); Debug.Assert(field != null, "We need to use an internal field named IsQuotedVersion that is declared on Cookie."); s_isQuotedVersionField = field; } @@ -582,9 +583,9 @@ namespace System.Net // Get // // Gets the next cookie or null if there are no more cookies. - internal Cookie Get() + internal Cookie? Get() { - Cookie cookie = null; + Cookie? cookie = null; // Only the first occurrence of an attribute value must be counted. bool commentSet = false; @@ -617,7 +618,7 @@ namespace System.Net if (!commentSet) { commentSet = true; - cookie.Comment = _tokenizer.Value; + cookie!.Comment = _tokenizer.Value; } break; @@ -625,9 +626,9 @@ namespace System.Net if (!commentUriSet) { commentUriSet = true; - if (Uri.TryCreate(CheckQuoted(_tokenizer.Value), UriKind.Absolute, out Uri parsed)) + if (Uri.TryCreate(CheckQuoted(_tokenizer.Value), UriKind.Absolute, out Uri? parsed)) { - cookie.CommentUri = parsed; + cookie!.CommentUri = parsed; } } break; @@ -636,7 +637,7 @@ namespace System.Net if (!domainSet) { domainSet = true; - cookie.Domain = CheckQuoted(_tokenizer.Value); + cookie!.Domain = CheckQuoted(_tokenizer.Value); IsQuotedDomainField.SetValue(cookie, _tokenizer.Quoted); } break; @@ -649,12 +650,12 @@ namespace System.Net if (DateTime.TryParse(CheckQuoted(_tokenizer.Value), CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out DateTime expires)) { - cookie.Expires = expires; + cookie!.Expires = expires; } else { // This cookie will be rejected - InternalSetNameMethod(cookie, string.Empty); + InternalSetNameMethod(cookie!, string.Empty); } } break; @@ -665,12 +666,12 @@ namespace System.Net expiresSet = true; if (int.TryParse(CheckQuoted(_tokenizer.Value), out int parsed)) { - cookie.Expires = DateTime.Now.AddSeconds(parsed); + cookie!.Expires = DateTime.Now.AddSeconds(parsed); } else { // This cookie will be rejected - InternalSetNameMethod(cookie, string.Empty); + InternalSetNameMethod(cookie!, string.Empty); } } break; @@ -679,7 +680,7 @@ namespace System.Net if (!pathSet) { pathSet = true; - cookie.Path = _tokenizer.Value; + cookie!.Path = _tokenizer.Value; } break; @@ -689,12 +690,12 @@ namespace System.Net portSet = true; try { - cookie.Port = _tokenizer.Value; + cookie!.Port = _tokenizer.Value; } catch { // This cookie will be rejected - InternalSetNameMethod(cookie, string.Empty); + InternalSetNameMethod(cookie!, string.Empty); } } break; @@ -706,13 +707,13 @@ namespace System.Net int parsed; if (int.TryParse(CheckQuoted(_tokenizer.Value), out parsed)) { - cookie.Version = parsed; + cookie!.Version = parsed; IsQuotedVersionField.SetValue(cookie, _tokenizer.Quoted); } else { // This cookie will be rejected - InternalSetNameMethod(cookie, string.Empty); + InternalSetNameMethod(cookie!, string.Empty); } } break; @@ -726,7 +727,7 @@ namespace System.Net if (!discardSet) { discardSet = true; - cookie.Discard = true; + cookie!.Discard = true; } break; @@ -734,19 +735,19 @@ namespace System.Net if (!secureSet) { secureSet = true; - cookie.Secure = true; + cookie!.Secure = true; } break; case CookieToken.HttpOnly: - cookie.HttpOnly = true; + cookie!.HttpOnly = true; break; case CookieToken.Port: if (!portSet) { portSet = true; - cookie.Port = string.Empty; + cookie!.Port = string.Empty; } break; } @@ -758,9 +759,9 @@ namespace System.Net return cookie; } - internal Cookie GetServer() + internal Cookie? GetServer() { - Cookie cookie = _savedCookie; + Cookie? cookie = _savedCookie; _savedCookie = null; // Only the first occurrence of an attribute value must be counted. @@ -793,7 +794,7 @@ namespace System.Net if (!domainSet) { domainSet = true; - cookie.Domain = CheckQuoted(_tokenizer.Value); + cookie!.Domain = CheckQuoted(_tokenizer.Value); IsQuotedDomainField.SetValue(cookie, _tokenizer.Quoted); } break; @@ -802,7 +803,7 @@ namespace System.Net if (!pathSet) { pathSet = true; - cookie.Path = _tokenizer.Value; + cookie!.Path = _tokenizer.Value; } break; @@ -812,12 +813,12 @@ namespace System.Net portSet = true; try { - cookie.Port = _tokenizer.Value; + cookie!.Port = _tokenizer.Value; } catch (CookieException) { // This cookie will be rejected - InternalSetNameMethod(cookie, string.Empty); + InternalSetNameMethod(cookie!, string.Empty); } } break; @@ -844,7 +845,7 @@ namespace System.Net if (_tokenizer.Token == CookieToken.Port && !portSet) { portSet = true; - cookie.Port = string.Empty; + cookie!.Port = string.Empty; } break; } 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 512f195..5a18dd6 100644 --- a/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs +++ b/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs @@ -57,7 +57,7 @@ namespace System.Net // See if it's an IP Address. IPHostEntry ipHostEntry; - if (IPAddress.TryParse(hostNameOrAddress, out IPAddress address)) + if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address)) { if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any)) { @@ -158,7 +158,7 @@ namespace System.Net // See if it's an IP Address. IPAddress[] addresses; - if (IPAddress.TryParse(hostNameOrAddress, out IPAddress address)) + if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address)) { if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any)) { @@ -210,7 +210,7 @@ namespace System.Net throw new ArgumentNullException(nameof(hostName)); } - if (IPAddress.TryParse(hostName, out IPAddress address)) + if (IPAddress.TryParse(hostName, out IPAddress? address)) { return CreateHostEntryForAddress(address); } @@ -287,7 +287,7 @@ namespace System.Net // See if it's an IP Address. IPHostEntry ipHostEntry; - if (IPAddress.TryParse(hostName, out IPAddress address) && + if (IPAddress.TryParse(hostName, out IPAddress? address) && (address.AddressFamily != AddressFamily.InterNetworkV6 || SocketProtocolSupportPal.OSSupportsIPv6)) { try @@ -441,7 +441,7 @@ namespace System.Net } // See if it's an IP Address. - if (IPAddress.TryParse(hostName, out IPAddress ipAddress)) + if (IPAddress.TryParse(hostName, out IPAddress? ipAddress)) { if (throwOnIIPAny && (ipAddress.Equals(IPAddress.Any) || ipAddress.Equals(IPAddress.IPv6Any))) { diff --git a/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.cs b/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.cs index 1388a8b..5567e60 100644 --- a/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.cs +++ b/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.cs @@ -208,7 +208,7 @@ namespace System.Net.NetworkInformation throw new ArgumentNullException(nameof(hostNameOrAddress)); } - if (IPAddress.TryParse(hostNameOrAddress, out IPAddress address)) + if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address)) { return Send(address, timeout, buffer, options); } @@ -357,7 +357,7 @@ namespace System.Net.NetworkInformation throw new ArgumentNullException(nameof(hostNameOrAddress)); } - if (IPAddress.TryParse(hostNameOrAddress, out IPAddress address)) + if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address)) { return SendPingAsync(address, timeout, buffer, options); } diff --git a/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs b/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs index 2bcad4d..b763e41 100644 --- a/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs +++ b/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs @@ -21,24 +21,30 @@ namespace System.Net public sealed partial class Cookie { public Cookie() { } - public Cookie(string name, string value) { } - public Cookie(string name, string value, string path) { } - public Cookie(string name, string value, string path, string domain) { } + public Cookie(string name, string? value) { } + public Cookie(string name, string? value, string? path) { } + public Cookie(string name, string? value, string? path, string? domain) { } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string Comment { get { throw null; } set { } } - public System.Uri CommentUri { get { throw null; } set { } } + public System.Uri? CommentUri { get { throw null; } set { } } public bool Discard { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string Domain { get { throw null; } set { } } public bool Expired { get { throw null; } set { } } public System.DateTime Expires { get { throw null; } set { } } public bool HttpOnly { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string Name { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string Path { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string Port { get { throw null; } set { } } public bool Secure { get { throw null; } set { } } public System.DateTime TimeStamp { get { throw null; } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string Value { get { throw null; } set { } } public int Version { get { throw null; } set { } } - public override bool Equals(object comparand) { throw null; } + public override bool Equals(object? comparand) { throw null; } public override int GetHashCode() { throw null; } public override string ToString() { throw null; } } @@ -49,7 +55,7 @@ namespace System.Net public bool IsReadOnly { get { throw null; } } public bool IsSynchronized { get { throw null; } } public System.Net.Cookie this[int index] { get { throw null; } } - public System.Net.Cookie this[string name] { get { throw null; } } + public System.Net.Cookie? this[string name] { get { throw null; } } public object SyncRoot { get { throw null; } } public void Add(System.Net.Cookie cookie) { } public void Add(System.Net.CookieCollection cookies) { } @@ -95,11 +101,11 @@ namespace System.Net public static System.Net.NetworkCredential DefaultNetworkCredentials { get { throw null; } } public void Add(string host, int port, string authenticationType, System.Net.NetworkCredential credential) { } public void Add(System.Uri uriPrefix, string authType, System.Net.NetworkCredential cred) { } - public System.Net.NetworkCredential GetCredential(string host, int port, string authenticationType) { throw null; } - public System.Net.NetworkCredential GetCredential(System.Uri uriPrefix, string authType) { throw null; } + public System.Net.NetworkCredential? GetCredential(string host, int port, string authenticationType) { throw null; } + public System.Net.NetworkCredential? GetCredential(System.Uri uriPrefix, string authType) { throw null; } public System.Collections.IEnumerator GetEnumerator() { throw null; } - public void Remove(string host, int port, string authenticationType) { } - public void Remove(System.Uri uriPrefix, string authType) { } + public void Remove(string? host, int port, string? authenticationType) { } + public void Remove(System.Uri? uriPrefix, string? authType) { } } [System.FlagsAttribute] public enum DecompressionMethods @@ -117,7 +123,7 @@ namespace System.Net public override System.Net.Sockets.AddressFamily AddressFamily { get { throw null; } } public string Host { get { throw null; } } public int Port { get { throw null; } } - public override bool Equals(object comparand) { throw null; } + public override bool Equals(object? comparand) { throw null; } public override int GetHashCode() { throw null; } public override string ToString() { throw null; } } @@ -207,11 +213,11 @@ namespace System.Net } public partial interface ICredentials { - System.Net.NetworkCredential GetCredential(System.Uri uri, string authType); + System.Net.NetworkCredential? GetCredential(System.Uri uri, string authType); } public partial interface ICredentialsByHost { - System.Net.NetworkCredential GetCredential(string host, int port, string authenticationType); + System.Net.NetworkCredential? GetCredential(string host, int port, string authenticationType); } public partial class IPAddress { @@ -236,7 +242,7 @@ namespace System.Net public bool IsIPv6SiteLocal { get { throw null; } } public bool IsIPv6Teredo { get { throw null; } } public long ScopeId { get { throw null; } set { } } - public override bool Equals(object comparand) { throw null; } + public override bool Equals(object? comparand) { throw null; } public byte[] GetAddressBytes() { throw null; } public override int GetHashCode() { throw null; } public static short HostToNetworkOrder(short host) { throw null; } @@ -252,8 +258,8 @@ namespace System.Net public static System.Net.IPAddress Parse(string ipString) { throw null; } public override string ToString() { throw null; } public bool TryFormat(System.Span destination, out int charsWritten) { throw null; } - public static bool TryParse(System.ReadOnlySpan ipString, out System.Net.IPAddress address) { throw null; } - public static bool TryParse(string ipString, out System.Net.IPAddress address) { throw null; } + public static bool TryParse(System.ReadOnlySpan ipString, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out System.Net.IPAddress? address) { throw null; } + public static bool TryParse(string? ipString, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out System.Net.IPAddress? address) { throw null; } public bool TryWriteBytes(System.Span destination, out int bytesWritten) { throw null; } } public partial class IPEndPoint : System.Net.EndPoint @@ -266,18 +272,18 @@ namespace System.Net public override System.Net.Sockets.AddressFamily AddressFamily { get { throw null; } } public int Port { get { throw null; } set { } } public override System.Net.EndPoint Create(System.Net.SocketAddress socketAddress) { throw null; } - public override bool Equals(object comparand) { throw null; } + public override bool Equals(object? comparand) { throw null; } public override int GetHashCode() { throw null; } public static System.Net.IPEndPoint Parse(System.ReadOnlySpan s) { throw null; } public static System.Net.IPEndPoint Parse(string s) { throw null; } public override System.Net.SocketAddress Serialize() { throw null; } public override string ToString() { throw null; } - public static bool TryParse(System.ReadOnlySpan s, out System.Net.IPEndPoint result) { throw null; } - public static bool TryParse(string s, out System.Net.IPEndPoint result) { throw null; } + public static bool TryParse(System.ReadOnlySpan s, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out System.Net.IPEndPoint result) { throw null; } + public static bool TryParse(string s, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out System.Net.IPEndPoint? result) { throw null; } } public partial interface IWebProxy { - System.Net.ICredentials Credentials { get; set; } + System.Net.ICredentials? Credentials { get; set; } System.Uri GetProxy(System.Uri destination); bool IsBypassed(System.Uri host); } @@ -285,18 +291,22 @@ namespace System.Net { public NetworkCredential() { } [System.CLSCompliantAttribute(false)] - public NetworkCredential(string userName, System.Security.SecureString password) { } + public NetworkCredential(string? userName, System.Security.SecureString? password) { } [System.CLSCompliantAttribute(false)] - public NetworkCredential(string userName, System.Security.SecureString password, string domain) { } - public NetworkCredential(string userName, string password) { } - public NetworkCredential(string userName, string password, string domain) { } + public NetworkCredential(string? userName, System.Security.SecureString? password, string? domain) { } + public NetworkCredential(string? userName, string? password) { } + public NetworkCredential(string? userName, string? password, string? domain) { } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string Domain { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string Password { get { throw null; } set { } } [System.CLSCompliantAttribute(false)] + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public System.Security.SecureString SecurePassword { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] public string UserName { get { throw null; } set { } } - public System.Net.NetworkCredential GetCredential(string host, int port, string authenticationType) { throw null; } - public System.Net.NetworkCredential GetCredential(System.Uri uri, string authType) { throw null; } + public System.Net.NetworkCredential GetCredential(string? host, int port, string? authenticationType) { throw null; } + public System.Net.NetworkCredential GetCredential(System.Uri? uri, string? authType) { throw null; } } public partial class SocketAddress { @@ -305,14 +315,14 @@ namespace System.Net public System.Net.Sockets.AddressFamily Family { get { throw null; } } public byte this[int offset] { get { throw null; } set { } } public int Size { get { throw null; } } - public override bool Equals(object comparand) { throw null; } + public override bool Equals(object? comparand) { throw null; } public override int GetHashCode() { throw null; } public override string ToString() { throw null; } } public abstract partial class TransportContext { protected TransportContext() { } - public abstract System.Security.Authentication.ExtendedProtection.ChannelBinding GetChannelBinding(System.Security.Authentication.ExtendedProtection.ChannelBindingKind kind); + public abstract System.Security.Authentication.ExtendedProtection.ChannelBinding? GetChannelBinding(System.Security.Authentication.ExtendedProtection.ChannelBindingKind kind); } } namespace System.Net.Cache diff --git a/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.csproj b/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.csproj index 71067fb..da2cdd8 100644 --- a/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.csproj +++ b/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.csproj @@ -1,6 +1,7 @@ $(NetCoreAppCurrent) + enable diff --git a/src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj b/src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj index 48e6f77..4d6d641 100644 --- a/src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj +++ b/src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj @@ -5,6 +5,7 @@ true false $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix + enable