MulticastOption.Group no longer accepts null. (#32518)
authorEric Erhardt <eric.erhardt@microsoft.com>
Sat, 22 Feb 2020 00:55:24 +0000 (18:55 -0600)
committerGitHub <noreply@github.com>
Sat, 22 Feb 2020 00:55:24 +0000 (18:55 -0600)
* MulticastOption.Group no longer accepts null.

MulticastOption.Group is not supposed to accept null. If someone sets it to null, we will NRE inside of the Sockets implementation.

I also fixed two small double-cast problems while I was in here.

Fix #32490

src/libraries/System.Net.Sockets/src/System/Net/Sockets/IPPacketInformation.cs
src/libraries/System.Net.Sockets/src/System/Net/Sockets/MulticastOption.cs
src/libraries/System.Net.Sockets/src/System/Net/Sockets/UdpReceiveResult.cs
src/libraries/System.Net.Sockets/tests/FunctionalTests/MulticastOptionTest.cs

index 89d1081..e4df577 100644 (file)
@@ -42,10 +42,8 @@ namespace System.Net.Sockets
             return !(packetInformation1 == packetInformation2);
         }
 
-        public override bool Equals(object comparand)
-        {
-            return comparand is IPPacketInformation && this == (IPPacketInformation)comparand;
-        }
+        public override bool Equals(object comparand) =>
+            comparand is IPPacketInformation other && this == other;
 
         public override int GetHashCode()
         {
index 161c412..5f413cf 100644 (file)
@@ -67,7 +67,7 @@ namespace System.Net.Sockets
             }
             set
             {
-                _group = value;
+                _group = value ?? throw new ArgumentNullException(nameof(value));
             }
         }
 
@@ -150,12 +150,7 @@ namespace System.Net.Sockets
             }
             set
             {
-                if (value == null)
-                {
-                    throw new ArgumentNullException(nameof(value));
-                }
-
-                _group = value;
+                _group = value ?? throw new ArgumentNullException(nameof(value));
             }
         }
 
index 2e94a73..02da108 100644 (file)
@@ -69,15 +69,8 @@ namespace System.Net.Sockets
         /// </summary>
         /// <param name="obj">The object to compare with this instance</param>
         /// <returns>true if obj is an instance of <see cref="UdpReceiveResult"/> and equals the value of the instance; otherwise, false</returns>
-        public override bool Equals(object obj)
-        {
-            if (!(obj is UdpReceiveResult))
-            {
-                return false;
-            }
-
-            return Equals((UdpReceiveResult)obj);
-        }
+        public override bool Equals(object obj) =>
+            obj is UdpReceiveResult other && Equals(other);
 
         /// <summary>
         /// Returns a value that indicates whether this instance is equal to a specified object
index 6b712b2..456a226 100644 (file)
@@ -26,8 +26,7 @@ namespace System.Net.Sockets.Tests
             var option = new MulticastOption(IPAddress.Any);
             Assert.Same(IPAddress.Any, option.Group);
 
-            option.Group = null;
-            Assert.Null(option.Group);
+            AssertExtensions.Throws<ArgumentNullException>("value", () => option.Group = null);
 
             option.Group = IPAddress.Broadcast;
             Assert.Same(IPAddress.Broadcast, option.Group);