Close the socket if bind() fails (dotnet/corefx#36769)
authorOmair Majid <omajid@redhat.com>
Mon, 15 Apr 2019 20:55:02 +0000 (16:55 -0400)
committerDavid Shulman <david.shulman@microsoft.com>
Mon, 15 Apr 2019 20:55:02 +0000 (13:55 -0700)
When using a socket with bind() and bind fails, close the socket to free
up the resources associated with the socket.

Commit migrated from https://github.com/dotnet/corefx/commit/03ae543d4da4e539b9915a5f38f36a69739fa9d9

src/libraries/Native/Unix/System.Native/pal_networkchange.c

index 822f7c8..dc1378c 100644 (file)
@@ -35,7 +35,9 @@ Error SystemNative_CreateNetworkChangeListenerSocket(int32_t* retSocket)
     if (bind(sock, (struct sockaddr*)(&sa), sizeof(sa)) != 0)
     {
         *retSocket = -1;
-        return (Error)(SystemNative_ConvertErrorPlatformToPal(errno));
+        Error palError = (Error)(SystemNative_ConvertErrorPlatformToPal(errno));
+        close(sock);
+        return palError;
     }
 
     *retSocket = sock;