Skip to content

Commit

Permalink
Annotate System.Net.Primitives for nullable (dotnet#31794)
Browse files Browse the repository at this point in the history
* Annotate System.Net.Primitives for nullable

* Fix build breaks in other libraries due to new annotations

Co-authored-by: Stephen Toub <[email protected]>
  • Loading branch information
iSazonov and stephentoub authored Feb 21, 2020
1 parent b357a47 commit b59225d
Show file tree
Hide file tree
Showing 22 changed files with 240 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal static unsafe string GetDomainName()
}

// Marshal.PtrToStringAnsi uses UTF8 on Unix.
return Marshal.PtrToStringAnsi((IntPtr)name);
return Marshal.PtrToStringAnsi((IntPtr)name)!;
}
}
}
79 changes: 40 additions & 39 deletions src/libraries/Common/src/System/Net/CookieParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,7 +46,7 @@ internal struct CookieTokenizer
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;
Expand Down Expand Up @@ -81,7 +82,7 @@ internal bool Eof
}
}

internal string Name
internal string? Name
{
get
{
Expand Down Expand Up @@ -449,7 +450,7 @@ internal CookieToken Token
}
}

internal bool IsEqualTo(string value)
internal bool IsEqualTo(string? value)
{
return string.Equals(_name, value, StringComparison.OrdinalIgnoreCase);
}
Expand Down Expand Up @@ -510,7 +511,7 @@ internal CookieToken TokenFromName(bool parseResponseCookies)
internal struct CookieParser
{
private CookieTokenizer _tokenizer;
private Cookie _savedCookie;
private Cookie? _savedCookie;

internal CookieParser(string cookieString)
{
Expand All @@ -519,13 +520,13 @@ internal CookieParser(string cookieString)
}

#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<Cookie, string, bool> s_internalSetNameMethod;
private static Func<Cookie, string, bool> InternalSetNameMethod
private static Func<Cookie, string?, bool>? s_internalSetNameMethod = null;
private static Func<Cookie, string?, bool> InternalSetNameMethod
{
get
{
Expand All @@ -535,25 +536,25 @@ private static Func<Cookie, string, bool> InternalSetNameMethod
// 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<Cookie, string, bool>)Delegate.CreateDelegate(typeof(Func<Cookie, string, bool>), method);
s_internalSetNameMethod = (Func<Cookie, string?, bool>)Delegate.CreateDelegate(typeof(Func<Cookie, string?, bool>), method);
}

return s_internalSetNameMethod;
}
}
#endif

private static FieldInfo s_isQuotedDomainField = null;
private static FieldInfo? s_isQuotedDomainField = null;
private static FieldInfo IsQuotedDomainField
{
get
{
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;
}
Expand All @@ -562,15 +563,15 @@ private static FieldInfo IsQuotedDomainField
}
}

private static FieldInfo s_isQuotedVersionField = null;
private static FieldInfo? s_isQuotedVersionField;
private static FieldInfo IsQuotedVersionField
{
get
{
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;
}
Expand All @@ -582,9 +583,9 @@ private static FieldInfo IsQuotedVersionField
// 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;
Expand Down Expand Up @@ -617,17 +618,17 @@ internal Cookie Get()
if (!commentSet)
{
commentSet = true;
cookie.Comment = _tokenizer.Value;
cookie!.Comment = _tokenizer.Value;
}
break;

case CookieToken.CommentUrl:
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;
Expand All @@ -636,7 +637,7 @@ internal Cookie Get()
if (!domainSet)
{
domainSet = true;
cookie.Domain = CheckQuoted(_tokenizer.Value);
cookie!.Domain = CheckQuoted(_tokenizer.Value);
IsQuotedDomainField.SetValue(cookie, _tokenizer.Quoted);
}
break;
Expand All @@ -649,12 +650,12 @@ internal Cookie Get()
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;
Expand All @@ -665,12 +666,12 @@ internal Cookie Get()
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;
Expand All @@ -679,7 +680,7 @@ internal Cookie Get()
if (!pathSet)
{
pathSet = true;
cookie.Path = _tokenizer.Value;
cookie!.Path = _tokenizer.Value;
}
break;

Expand All @@ -689,12 +690,12 @@ internal Cookie Get()
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;
Expand All @@ -706,13 +707,13 @@ internal Cookie Get()
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;
Expand All @@ -726,27 +727,27 @@ internal Cookie Get()
if (!discardSet)
{
discardSet = true;
cookie.Discard = true;
cookie!.Discard = true;
}
break;

case CookieToken.Secure:
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;
}
Expand All @@ -758,9 +759,9 @@ internal Cookie Get()
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.
Expand Down Expand Up @@ -793,7 +794,7 @@ internal Cookie GetServer()
if (!domainSet)
{
domainSet = true;
cookie.Domain = CheckQuoted(_tokenizer.Value);
cookie!.Domain = CheckQuoted(_tokenizer.Value);
IsQuotedDomainField.SetValue(cookie, _tokenizer.Quoted);
}
break;
Expand All @@ -802,7 +803,7 @@ internal Cookie GetServer()
if (!pathSet)
{
pathSet = true;
cookie.Path = _tokenizer.Value;
cookie!.Path = _tokenizer.Value;
}
break;

Expand All @@ -812,12 +813,12 @@ internal Cookie GetServer()
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;
Expand All @@ -844,7 +845,7 @@ internal Cookie GetServer()
if (_tokenizer.Token == CookieToken.Port && !portSet)
{
portSet = true;
cookie.Port = string.Empty;
cookie!.Port = string.Empty;
}
break;
}
Expand Down
10 changes: 5 additions & 5 deletions src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static IPHostEntry GetHostEntry(string hostNameOrAddress)

// 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))
{
Expand Down Expand Up @@ -158,7 +158,7 @@ public static IPAddress[] GetHostAddresses(string hostNameOrAddress)

// 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))
{
Expand Down Expand Up @@ -210,7 +210,7 @@ public static IPHostEntry GetHostByName(string hostName)
throw new ArgumentNullException(nameof(hostName));
}

if (IPAddress.TryParse(hostName, out IPAddress address))
if (IPAddress.TryParse(hostName, out IPAddress? address))
{
return CreateHostEntryForAddress(address);
}
Expand Down Expand Up @@ -287,7 +287,7 @@ public static IPHostEntry Resolve(string hostName)

// 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
Expand Down Expand Up @@ -441,7 +441,7 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
}

// 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)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public PingReply Send(string hostNameOrAddress, int timeout, byte[] buffer, Ping
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);
}
Expand Down Expand Up @@ -357,7 +357,7 @@ public Task<PingReply> SendPingAsync(string hostNameOrAddress, int timeout, byte
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);
}
Expand Down
Loading

0 comments on commit b59225d

Please sign in to comment.