From 43b90ace98650b1e3074c3dd967022bae16509ff Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 23 Feb 2018 16:49:26 +0000 Subject: [PATCH] Re-enable initlocals clearing in System.Net.Sockets on Unix We disabled it because some ReceiveMessageFrom tests were failing. The issue turned out to be that we weren't explicitly clearing an IP packet information struct before handing it to native code, and the native code wasn't clearing it. This fixes that. Along with it, I initialized a few other variables, just for good measure. --- .../src/System.Net.Sockets.csproj | 2 +- .../src/System/Net/Sockets/SocketPal.Unix.cs | 22 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/System.Net.Sockets/src/System.Net.Sockets.csproj b/src/System.Net.Sockets/src/System.Net.Sockets.csproj index b27b2d9e6889..e29864b9aab1 100644 --- a/src/System.Net.Sockets/src/System.Net.Sockets.csproj +++ b/src/System.Net.Sockets/src/System.Net.Sockets.csproj @@ -7,7 +7,7 @@ true true true - true + true diff --git a/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs b/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs index 3367ac02594a..bd2d7b490365 100644 --- a/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs +++ b/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs @@ -47,7 +47,7 @@ private static unsafe IPPacketInformation GetIPPacketInformation(Interop.Sys.Mes return default(IPPacketInformation); } - Interop.Sys.IPPacketInformation nativePacketInfo; + Interop.Sys.IPPacketInformation nativePacketInfo = default; if (!Interop.Sys.TryGetIPPacketInformation(messageHeader, isIPv4, &nativePacketInfo)) { return default(IPPacketInformation); @@ -65,7 +65,7 @@ private static unsafe int Receive(SafeCloseSocket socket, SocketFlags flags, Spa { Debug.Assert(socketAddress != null || socketAddressLen == 0, $"Unexpected values: socketAddress={socketAddress}, socketAddressLen={socketAddressLen}"); - long received; + long received = 0; int sockAddrLen = socketAddress != null ? socketAddressLen : 0; fixed (byte* sockAddr = socketAddress) @@ -123,7 +123,7 @@ private static unsafe int Send(SafeCloseSocket socket, SocketFlags flags, ReadOn IOVectorCount = 1 }; - long bytesSent; + long bytesSent = 0; errno = Interop.Sys.SendMessage( socket.DangerousGetHandle(), // to minimize chances of handle recycling from misuse, this should use DangerousAddRef/Release, but it adds too much overhead &messageHeader, @@ -186,7 +186,7 @@ private static unsafe int Send(SafeCloseSocket socket, SocketFlags flags, IList< IOVectorCount = iovCount }; - long bytesSent; + long bytesSent = 0; errno = Interop.Sys.SendMessage( socket.DangerousGetHandle(), // to minimize chances of handle recycling from misuse, this should use DangerousAddRef/Release, but it adds too much overhead &messageHeader, @@ -244,7 +244,7 @@ private static unsafe long SendFile(SafeCloseSocket socket, SafeFileHandle fileH private static unsafe int Receive(SafeCloseSocket socket, SocketFlags flags, IList> buffers, byte[] socketAddress, ref int socketAddressLen, out SocketFlags receivedFlags, out Interop.Error errno) { - int available; + int available = 0; errno = Interop.Sys.GetBytesAvailable(socket, &available); if (errno != Interop.Error.SUCCESS) { @@ -353,7 +353,7 @@ private static unsafe int ReceiveMessageFrom(SafeCloseSocket socket, SocketFlags Interop.Sys.MessageHeader messageHeader; - long received; + long received = 0; fixed (byte* rawSocketAddress = socketAddress) fixed (byte* b = &MemoryMarshal.GetReference(buffer)) { @@ -432,7 +432,7 @@ private static unsafe int ReceiveMessageFrom( ControlBufferLen = cmsgBufferLen }; - long received; + long received = 0; errno = Interop.Sys.ReceiveMessage( socket.DangerousGetHandle(), // to minimize chances of handle recycling from misuse, this should use DangerousAddRef/Release, but it adds too much overhead &messageHeader, @@ -470,7 +470,7 @@ private static unsafe int ReceiveMessageFrom( public static unsafe bool TryCompleteAccept(SafeCloseSocket socket, byte[] socketAddress, ref int socketAddressLen, out IntPtr acceptedFd, out SocketError errorCode) { - IntPtr fd; + IntPtr fd = IntPtr.Zero; Interop.Error errno; int sockAddrLen = socketAddressLen; fixed (byte* rawSocketAddress = socketAddress) @@ -545,7 +545,7 @@ public static unsafe bool TryStartConnect(SafeCloseSocket socket, byte[] socketA public static unsafe bool TryCompleteConnect(SafeCloseSocket socket, int socketAddressLen, out SocketError errorCode) { - Interop.Error socketError; + Interop.Error socketError = default; Interop.Error err; try { @@ -1307,7 +1307,7 @@ public static unsafe SocketError GetMulticastOption(SafeCloseSocket handle, Sock Interop.Sys.MulticastOption.MULTICAST_ADD : Interop.Sys.MulticastOption.MULTICAST_DROP; - Interop.Sys.IPv4MulticastOption opt; + Interop.Sys.IPv4MulticastOption opt = default; Interop.Error err = Interop.Sys.GetIPv4MulticastOption(handle, optName, &opt); if (err != Interop.Error.SUCCESS) { @@ -1332,7 +1332,7 @@ public static unsafe SocketError GetIPv6MulticastOption(SafeCloseSocket handle, Interop.Sys.MulticastOption.MULTICAST_ADD : Interop.Sys.MulticastOption.MULTICAST_DROP; - Interop.Sys.IPv6MulticastOption opt; + Interop.Sys.IPv6MulticastOption opt = default; Interop.Error err = Interop.Sys.GetIPv6MulticastOption(handle, optName, &opt); if (err != Interop.Error.SUCCESS) {