diff options
author | Torsten Rupp <rupp@aicas.com> | 2003-04-30 14:10:31 +0000 |
---|---|---|
committer | Torsten Rupp <rupp@aicas.com> | 2003-04-30 14:10:31 +0000 |
commit | 6b72f5d1ec065f5206470bd31be7ddc6742749d4 (patch) | |
tree | 7530bb6c44975de3943a5d34b6e3a2196eb572c7 | |
parent | 6466694184b2821555277780eaf2ba7758cd42ed (diff) | |
download | classpath-6b72f5d1ec065f5206470bd31be7ddc6742749d4.tar.gz |
Added try-catch-blocks to
* Socket.java: socket()
* ServerSocket.java: socket()
* DatagramSocket.java: socket()
with clean-up of file-descriptors in case bind(), connect() or listen()
fail. This fix will avoid loss of file-descriptors in the case create()
is executed successfully, but bind/connect/listen fail with an exception.
Then close() was not called and the file-descriptor allcoated by create()
was not returned to the OS.
-rw-r--r-- | ChangeLog | 15 | ||||
-rw-r--r-- | java/net/DatagramSocket.java | 14 | ||||
-rw-r--r-- | java/net/ServerSocket.java | 31 | ||||
-rw-r--r-- | java/net/Socket.java | 35 |
4 files changed, 86 insertions, 9 deletions
@@ -1,3 +1,18 @@ +2003-04-30 Torsten Rupp <rupp@homer.aicas.com> + + * /cvsroot/classpath/classpath/java/net/Socket.java: + Added try-catch-blocks to + + * Socket.java: socket() + * ServerSocket.java: socket() + * DatagramSocket.java: socket() + + with clean-up of file-descriptors in case bind(), connect() or listen() + fail. This fix will avoid loss of file-descriptors in the case create() + is executed successfully, but bind/connect/listen fail with an exception. + Then close() was not called and the file-descriptor allcoated by create() + was not returned to the OS. + 2003-04-30 Michael Koch <konqueror@gmx.de> * java/security/cert/Certificate.java diff --git a/java/net/DatagramSocket.java b/java/net/DatagramSocket.java index 674dca268..831455631 100644 --- a/java/net/DatagramSocket.java +++ b/java/net/DatagramSocket.java @@ -181,11 +181,21 @@ public class DatagramSocket impl = new PlainDatagramSocketImpl(); impl.create(); - if (address != null) - { + if (address != null) { + try { local_addr = tmp.getAddress (); impl.bind(tmp.getPort (), tmp.getAddress ()); + } catch (SocketException exception) { + impl.close(); + throw exception; + } catch (RuntimeException exception) { + impl.close(); + throw exception; + } catch (Error error) { + impl.close(); + throw error; } + } } /** diff --git a/java/net/ServerSocket.java b/java/net/ServerSocket.java index 0285c128c..b6cc5e1db 100644 --- a/java/net/ServerSocket.java +++ b/java/net/ServerSocket.java @@ -164,9 +164,36 @@ public class ServerSocket if (bindAddr == null) bindAddr = InetAddress.ANY_IF; + // create socket impl.create(true); - impl.bind(bindAddr, port); - impl.listen(backlog); + + // bind to address/port + try { + impl.bind(bindAddr, port); + } catch (IOException exception) { + impl.close(); + throw exception; + } catch (RuntimeException exception) { + impl.close(); + throw exception; + } catch (Error error) { + impl.close(); + throw error; + } + + // listen on socket + try { + impl.listen(backlog); + } catch (IOException exception) { + impl.close(); + throw exception; + } catch (RuntimeException exception) { + impl.close(); + throw exception; + } catch (Error error) { + impl.close(); + throw error; + } } /** diff --git a/java/net/Socket.java b/java/net/Socket.java index 7070838c7..d30cebc0c 100644 --- a/java/net/Socket.java +++ b/java/net/Socket.java @@ -291,16 +291,41 @@ public class Socket if (sm != null) sm.checkConnect(raddr.getHostName(), rport); + // create socket impl.create(stream); + // bind/connect to address/port // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port, // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as // that default. JDK 1.2 doc infers not to do a bind. - if (laddr != null) - impl.bind(laddr, lport); - - if (raddr != null) - impl.connect(raddr, rport); + if (laddr != null) { + try { + impl.bind(laddr, lport); + } catch (IOException exception) { + impl.close(); + throw exception; + } catch (RuntimeException exception) { + impl.close(); + throw exception; + } catch (Error error) { + impl.close(); + throw error; + } + } + if (raddr != null) { + try { + impl.connect(raddr, rport); + } catch (IOException exception) { + impl.close(); + throw exception; + } catch (RuntimeException exception) { + impl.close(); + throw exception; + } catch (Error error) { + impl.close(); + throw error; + } + } } /** |