From fc37561e320b46d277cd404dda7a60c0063e0f97 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Fri, 21 Feb 2020 18:55:24 -0600 Subject: [PATCH] MulticastOption.Group no longer accepts null. (#32518) * 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/System/Net/Sockets/IPPacketInformation.cs | 6 ++---- .../src/System/Net/Sockets/MulticastOption.cs | 9 ++------- .../src/System/Net/Sockets/UdpReceiveResult.cs | 11 ++--------- .../tests/FunctionalTests/MulticastOptionTest.cs | 3 +-- 4 files changed, 7 insertions(+), 22 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/IPPacketInformation.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/IPPacketInformation.cs index 89d1081..e4df577 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/IPPacketInformation.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/IPPacketInformation.cs @@ -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() { diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/MulticastOption.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/MulticastOption.cs index 161c412..5f413cf0 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/MulticastOption.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/MulticastOption.cs @@ -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)); } } diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UdpReceiveResult.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UdpReceiveResult.cs index 2e94a73..02da108 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UdpReceiveResult.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UdpReceiveResult.cs @@ -69,15 +69,8 @@ namespace System.Net.Sockets /// /// The object to compare with this instance /// true if obj is an instance of and equals the value of the instance; otherwise, false - 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); /// /// Returns a value that indicates whether this instance is equal to a specified object diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/MulticastOptionTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/MulticastOptionTest.cs index 6b712b2..456a226 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/MulticastOptionTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/MulticastOptionTest.cs @@ -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("value", () => option.Group = null); option.Group = IPAddress.Broadcast; Assert.Same(IPAddress.Broadcast, option.Group); -- 2.7.4