changeset 4560:e8eee45e1ca5

7085981: XXSocket types depend on impl finalizer to close if constructor throws exception Reviewed-by: alanb, chegar
author michaelm
date Fri, 09 Sep 2011 14:04:44 +0100
parents 6dab08f1cabb
children 0ba4b29c7d9a
files src/share/classes/java/net/DatagramSocket.java src/share/classes/java/net/MulticastSocket.java src/share/classes/java/net/Socket.java
diffstat 3 files changed, 17 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/net/DatagramSocket.java	Thu Sep 08 09:04:28 2011 +0800
+++ b/src/share/classes/java/net/DatagramSocket.java	Fri Sep 09 14:04:44 2011 +0100
@@ -174,9 +174,7 @@
      * @see SecurityManager#checkListen
      */
     public DatagramSocket() throws SocketException {
-        // create a datagram socket.
-        createImpl();
-        bind(new InetSocketAddress(0));
+        this(new InetSocketAddress(0));
     }
 
     /**
@@ -221,7 +219,12 @@
         // create a datagram socket.
         createImpl();
         if (bindaddr != null) {
-            bind(bindaddr);
+            try {
+                bind(bindaddr);
+            } finally {
+                if (!isBound())
+                    close();
+            }
         }
     }
 
--- a/src/share/classes/java/net/MulticastSocket.java	Thu Sep 08 09:04:28 2011 +0800
+++ b/src/share/classes/java/net/MulticastSocket.java	Fri Sep 09 14:04:44 2011 +0100
@@ -162,7 +162,12 @@
         setReuseAddress(true);
 
         if (bindaddr != null) {
-            bind(bindaddr);
+            try {
+                bind(bindaddr);
+            } finally {
+                if (!isBound())
+                    close();
+            }
         }
     }
 
--- a/src/share/classes/java/net/Socket.java	Thu Sep 08 09:04:28 2011 +0800
+++ b/src/share/classes/java/net/Socket.java	Fri Sep 09 14:04:44 2011 +0100
@@ -425,6 +425,10 @@
         } catch (IOException e) {
             close();
             throw e;
+        } finally {
+            // if bind() or connect threw a runtime exception
+            if ((localAddr != null && !bound) || (address != null && !connected))
+                close();
         }
     }