From: Bryce McKinlay Date: Thu, 7 Aug 2003 06:30:16 +0000 (+0000) Subject: re PR libgcj/10868 (java.net.ServerSocket's constructors create and leak extra sockets) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=62ad7de1e049583a413e9bfe7fbbbf02ab2b1aea;p=gcc.git re PR libgcj/10868 (java.net.ServerSocket's constructors create and leak extra sockets) * java/net/Socket.java (Socket (SocketImpl)): Don't allow null SocketImpl. Update Javadoc. (bind): Call close() not impl.close() in event of exception. (connect): Likewise. Remove superfluous null checks throughout. * java/net/ServerSocket.java (ServerSocket (int, int, InetAddress)): Don't create an extra socket. Fix for PR libgcj/10868. (bind): Clean up exception handling. Remove superfluous null checks throughout. From-SVN: r70219 --- diff --git a/libjava/ChangeLog b/libjava/ChangeLog index e773c70c6f0..25cc0b9e314 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,15 @@ +2003-08-07 Bryce McKinlay + + * java/net/Socket.java (Socket (SocketImpl)): Don't allow null + SocketImpl. Update Javadoc. + (bind): Call close() not impl.close() in event of exception. + (connect): Likewise. + Remove superfluous null checks throughout. + * java/net/ServerSocket.java (ServerSocket (int, int, InetAddress)): + Don't create an extra socket. Fix for PR libgcj/10868. + (bind): Clean up exception handling. + Remove superfluous null checks throughout. + 2003-08-07 Jacob Gladish Bryce McKinlay diff --git a/libjava/java/net/ServerSocket.java b/libjava/java/net/ServerSocket.java index 699319e70b2..3614354f227 100644 --- a/libjava/java/net/ServerSocket.java +++ b/libjava/java/net/ServerSocket.java @@ -154,12 +154,6 @@ public class ServerSocket { this(); - if (impl == null) - throw new IOException("Cannot initialize Socket implementation"); - - // create socket - impl.create(true); - // bind/listen socket bind (new InetSocketAddress (bindAddr, port), backlog); } @@ -208,9 +202,6 @@ public class ServerSocket if (closed) throw new SocketException ("ServerSocket is closed"); - if (impl == null) - throw new IOException ("Cannot initialize Socket implementation"); - if (! (endpoint instanceof InetSocketAddress)) throw new IllegalArgumentException ("Address type not supported"); @@ -220,45 +211,24 @@ public class ServerSocket if (s != null) s.checkListen (tmp.getPort ()); - // bind to address/port try { - impl.bind (tmp.getAddress (), tmp.getPort ()); + impl.bind (tmp.getAddress (), tmp.getPort ()); + impl.listen(backlog); } catch (IOException exception) { - impl.close(); + close(); throw exception; } catch (RuntimeException exception) { - impl.close(); + 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(); + close(); throw error; } } @@ -320,9 +290,6 @@ public class ServerSocket */ public Socket accept () throws IOException { - if (impl == null) - throw new IOException ("Cannot initialize Socket implementation"); - SecurityManager sm = System.getSecurityManager (); if (sm != null) sm.checkListen (impl.getLocalPort ()); @@ -466,9 +433,6 @@ public class ServerSocket public void setReuseAddress (boolean on) throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); - impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); } @@ -482,9 +446,6 @@ public class ServerSocket public boolean getReuseAddress() throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); - Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR); if (!(reuseaddr instanceof Boolean)) @@ -508,9 +469,6 @@ public class ServerSocket public void setReceiveBufferSize (int size) throws SocketException { - if (impl == null) - throw new SocketException ("Not connected"); - if (size <= 0) throw new IllegalArgumentException ("SO_RCVBUF value must be > 0"); @@ -531,9 +489,6 @@ public class ServerSocket public int getReceiveBufferSize () throws SocketException { - if (impl == null) - throw new SocketException ("Not connected"); - Object buf = impl.getOption (SocketOptions.SO_RCVBUF); if (!(buf instanceof Integer)) diff --git a/libjava/java/net/Socket.java b/libjava/java/net/Socket.java index ef88de5c151..c2615164d39 100644 --- a/libjava/java/net/Socket.java +++ b/libjava/java/net/Socket.java @@ -82,8 +82,8 @@ public class Socket */ SocketImpl impl; - private boolean inputShutdown; - private boolean outputShutdown; + private boolean inputShutdown = false; + private boolean outputShutdown = false; SocketChannel ch; // this field must have been set if created by SocketChannel @@ -103,9 +103,6 @@ public class Socket impl = factory.createSocketImpl(); else impl = new PlainSocketImpl(); - - inputShutdown = false; - outputShutdown = false; } /** @@ -115,9 +112,8 @@ public class Socket *

* Additionally, this socket will be created using the supplied * implementation class instead the default class or one returned by a - * factory. This value can be null, but if it is, all instance - * methods in Socket should be overridden because most of them - * rely on this value being populated. + * factory. If this value is null, the default Socket + * implementation is used. * * @param impl The SocketImpl to use for this * Socket @@ -128,9 +124,10 @@ public class Socket */ protected Socket (SocketImpl impl) throws SocketException { - this.impl = impl; - this.inputShutdown = false; - this.outputShutdown = false; + if (impl == null) + this.impl = new PlainSocketImpl(); + else + this.impl = impl; } /** @@ -282,12 +279,6 @@ public class Socket { this(); - if (raddr == null) - throw new NullPointerException (); - - if (impl == null) - throw new IOException("Cannot initialize Socket implementation"); - SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkConnect(raddr.getHostName(), rport); @@ -351,17 +342,17 @@ public class Socket } catch (IOException exception) { - impl.close (); + close (); throw exception; } catch (RuntimeException exception) { - impl.close (); + close (); throw exception; } catch (Error error) { - impl.close (); + close (); throw error; } } @@ -420,17 +411,17 @@ public class Socket } catch (IOException exception) { - impl.close (); + close (); throw exception; } catch (RuntimeException exception) { - impl.close (); + close (); throw exception; } catch (Error error) { - impl.close (); + close (); throw error; } } @@ -443,10 +434,7 @@ public class Socket */ public InetAddress getInetAddress () { - if (impl != null) - return impl.getInetAddress(); - - return null; + return impl.getInetAddress(); } /** @@ -459,9 +447,6 @@ public class Socket */ public InetAddress getLocalAddress () { - if (impl == null) - return null; - InetAddress addr = null; try { @@ -586,9 +571,6 @@ public class Socket */ public void setTcpNoDelay (boolean on) throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on)); } @@ -606,9 +588,6 @@ public class Socket */ public boolean getTcpNoDelay() throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - Object on = impl.getOption(SocketOptions.TCP_NODELAY); if (on instanceof Boolean) @@ -636,9 +615,6 @@ public class Socket */ public void setSoLinger(boolean on, int linger) throws SocketException { - if (impl == null) - throw new SocketException("No socket created"); - if (on == true) { if (linger < 0) @@ -673,9 +649,6 @@ public class Socket */ public int getSoLinger() throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - Object linger = impl.getOption(SocketOptions.SO_LINGER); if (linger instanceof Integer) return(((Integer)linger).intValue()); @@ -709,9 +682,6 @@ public class Socket */ public void setOOBInline (boolean on) throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on)); } @@ -724,9 +694,6 @@ public class Socket */ public boolean getOOBInline () throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - Object buf = impl.getOption(SocketOptions.SO_OOBINLINE); if (buf instanceof Boolean) @@ -754,9 +721,6 @@ public class Socket */ public synchronized void setSoTimeout (int timeout) throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - if (timeout < 0) throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0"); @@ -782,9 +746,6 @@ public class Socket */ public synchronized int getSoTimeout () throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT); if (timeout instanceof Integer) return(((Integer)timeout).intValue()); @@ -806,9 +767,6 @@ public class Socket */ public void setSendBufferSize (int size) throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - if (size <= 0) throw new IllegalArgumentException("SO_SNDBUF value must be > 0"); @@ -828,9 +786,6 @@ public class Socket */ public int getSendBufferSize () throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - Object buf = impl.getOption(SocketOptions.SO_SNDBUF); if (buf instanceof Integer) @@ -853,9 +808,6 @@ public class Socket */ public void setReceiveBufferSize (int size) throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - if (size <= 0) throw new IllegalArgumentException("SO_RCVBUF value must be > 0"); @@ -875,9 +827,6 @@ public class Socket */ public int getReceiveBufferSize () throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - Object buf = impl.getOption(SocketOptions.SO_RCVBUF); if (buf instanceof Integer) @@ -898,9 +847,6 @@ public class Socket */ public void setKeepAlive (boolean on) throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on)); } @@ -916,9 +862,6 @@ public class Socket */ public boolean getKeepAlive () throws SocketException { - if (impl == null) - throw new SocketException("Not connected"); - Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE); if (buf instanceof Boolean) @@ -1037,9 +980,6 @@ public class Socket */ public boolean getReuseAddress () throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); - Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR); if (!(reuseaddr instanceof Boolean)) @@ -1057,9 +997,6 @@ public class Socket */ public void setReuseAddress (boolean on) throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); - impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); } @@ -1074,9 +1011,6 @@ public class Socket */ public int getTrafficClass () throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); - Object obj = impl.getOption(SocketOptions.IP_TOS); if (obj instanceof Integer) @@ -1099,9 +1033,6 @@ public class Socket */ public void setTrafficClass (int tc) throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); - if (tc < 0 || tc > 255) throw new IllegalArgumentException();