提交 393c00fe 编写于 作者: M michaelm

7085981: XXSocket types depend on impl finalizer to close if constructor throws exception

Reviewed-by: alanb, chegar
上级 6a8f08cb
...@@ -174,9 +174,7 @@ class DatagramSocket implements java.io.Closeable { ...@@ -174,9 +174,7 @@ class DatagramSocket implements java.io.Closeable {
* @see SecurityManager#checkListen * @see SecurityManager#checkListen
*/ */
public DatagramSocket() throws SocketException { public DatagramSocket() throws SocketException {
// create a datagram socket. this(new InetSocketAddress(0));
createImpl();
bind(new InetSocketAddress(0));
} }
/** /**
...@@ -221,7 +219,12 @@ class DatagramSocket implements java.io.Closeable { ...@@ -221,7 +219,12 @@ class DatagramSocket implements java.io.Closeable {
// create a datagram socket. // create a datagram socket.
createImpl(); createImpl();
if (bindaddr != null) { if (bindaddr != null) {
try {
bind(bindaddr); bind(bindaddr);
} finally {
if (!isBound())
close();
}
} }
} }
......
...@@ -162,7 +162,12 @@ class MulticastSocket extends DatagramSocket { ...@@ -162,7 +162,12 @@ class MulticastSocket extends DatagramSocket {
setReuseAddress(true); setReuseAddress(true);
if (bindaddr != null) { if (bindaddr != null) {
try {
bind(bindaddr); bind(bindaddr);
} finally {
if (!isBound())
close();
}
} }
} }
......
...@@ -425,6 +425,10 @@ class Socket implements java.io.Closeable { ...@@ -425,6 +425,10 @@ class Socket implements java.io.Closeable {
} catch (IOException e) { } catch (IOException e) {
close(); close();
throw e; throw e;
} finally {
// if bind() or connect threw a runtime exception
if ((localAddr != null && !bound) || (address != null && !connected))
close();
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册