From 9f0fd3bdbd84375ed1fd4bcef94b8e19e3961f68 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Tue, 18 Feb 2020 18:21:19 -0600 Subject: [PATCH 1/2] 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 | 2 +- .../src/System/Net/Sockets/MulticastOption.cs | 5 +++++ .../src/System/Net/Sockets/UdpReceiveResult.cs | 4 ++-- .../tests/FunctionalTests/MulticastOptionTest.cs | 3 +-- 4 files changed, 9 insertions(+), 5 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 89d1081228978b..b27c158e0567c4 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 @@ -44,7 +44,7 @@ public int Interface public override bool Equals(object comparand) { - return comparand is IPPacketInformation && this == (IPPacketInformation)comparand; + return 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 161c41249d286e..e7e6da62676634 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,6 +67,11 @@ public IPAddress Group } set { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + _group = 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 2e94a735933e08..b47fed03eeb37d 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 @@ -71,12 +71,12 @@ public override int GetHashCode() /// 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)) + if (!(obj is UdpReceiveResult other)) { return false; } - return Equals((UdpReceiveResult)obj); + return Equals(other); } /// diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/MulticastOptionTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/MulticastOptionTest.cs index 6b712b21026ae3..906c5e3aecfafb 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 @@ public void MulticastOption_Group_Roundtrips() var option = new MulticastOption(IPAddress.Any); Assert.Same(IPAddress.Any, option.Group); - option.Group = null; - Assert.Null(option.Group); + Assert.Throws("value", () => option.Group = null); option.Group = IPAddress.Broadcast; Assert.Same(IPAddress.Broadcast, option.Group); From 8cbe17097cedaa2499400c9ff60f67c56a83dfb0 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Wed, 19 Feb 2020 09:27:13 -0600 Subject: [PATCH 2/2] Address PR feedback --- .../src/System/Net/Sockets/IPPacketInformation.cs | 6 ++---- .../src/System/Net/Sockets/MulticastOption.cs | 14 ++------------ .../src/System/Net/Sockets/UdpReceiveResult.cs | 11 ++--------- .../tests/FunctionalTests/MulticastOptionTest.cs | 2 +- 4 files changed, 7 insertions(+), 26 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 b27c158e0567c4..e4df5776b93e1b 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 @@ public int Interface return !(packetInformation1 == packetInformation2); } - public override bool Equals(object comparand) - { - return comparand is IPPacketInformation other && this == other; - } + 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 e7e6da62676634..5f413cf06cfc0b 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,12 +67,7 @@ public IPAddress Group } set { - if (value == null) - { - throw new ArgumentNullException(nameof(value)); - } - - _group = value; + _group = value ?? throw new ArgumentNullException(nameof(value)); } } @@ -155,12 +150,7 @@ public IPAddress Group } 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 b47fed03eeb37d..02da108c787ef7 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 @@ public override int GetHashCode() /// /// 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 other)) - { - return false; - } - - return Equals(other); - } + 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 906c5e3aecfafb..456a2268e6c479 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/MulticastOptionTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/MulticastOptionTest.cs @@ -26,7 +26,7 @@ public void MulticastOption_Group_Roundtrips() var option = new MulticastOption(IPAddress.Any); Assert.Same(IPAddress.Any, option.Group); - Assert.Throws("value", () => option.Group = null); + AssertExtensions.Throws("value", () => option.Group = null); option.Group = IPAddress.Broadcast; Assert.Same(IPAddress.Broadcast, option.Group);