This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: Patch: Account for possible null laddr in Socket.java
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Am Dienstag, 17. Juni 2003 14:19 schrieb Michael Koch:
> Am Dienstag, 17. Juni 2003 06:10 schrieb Mohan Embar:
> > Hi Michael,
> >
> > >> I'm reading the J2SE 1.4.0 documentation and don't see
> > >> anywhere that raddr is allowed to be null. In fact, the
> > >> documentation for Socket(InetAddress, int, boolean) seems to
> > >> imply the contrary (cf. "If there is a security manager, its
> > >> checkConnect method is called with host.getHostAddress() and
> > >> port as its arguments.").
> > >>
> > >> Nor do I see that we are explicitly passing an raddr==null
> > >> from one constructor call to another.
> > >>
> > >> Am I missing something?
> > >
> > >Well, I should better explane this.
> > >
> > >The currect version is like this:
> > >
> > >connect (new InetSocketAddress (raddr, rport));
> > >
> > >If raddr is null then InetSocketAddress will assign the wildcard
> > >address.
> > >
> > >connect cannot connect to a wildcard address. IOException.
> >
> > No, if raddr is null then a NullPointerException gets thrown
> > in the InetSocketAddress constructor via addr.getHostName()
>
> Then there is a bug too, null means wildcard address. I will fis
> this.
I have written a new patch for this. Please review and comment.
Michael
- --
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
iD8DBQE+7x4oWSOgCCdjSDsRAiH+AJ9k6zwE5ljjA9xjGyACGIwwjgSYrgCgiaj2
v39k/KTSywAEdLMWix7vBuo=
=/elN
-----END PGP SIGNATURE-----
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.1951
diff -u -b -B -r1.1951 ChangeLog
--- ChangeLog 17 Jun 2003 13:01:19 -0000 1.1951
+++ ChangeLog 17 Jun 2003 13:52:23 -0000
@@ -1,5 +1,16 @@
2003-06-17 Michael Koch <konqueror@gmx.de>
+ * java/net/InetSocketAddress.java
+ (InetSocketAddress): Use wildcard address if addr is null.
+ (InetSocketAddress): Dont duplicate implementation.
+ (InetSocketAddress): Throw exception when hostname is null.
+ * java/net/Socket.java:
+ Reworked imports.
+ (Socket): Throw exception when raddr is null, handle case when laddr
+ is null.
+
+2003-06-17 Michael Koch <konqueror@gmx.de>
+
* java/util/prefs/AbstractPreferences.java,
java/util/prefs/PreferencesFactory.java:
Reworked imports, removed unused imports.
Index: java/net/InetSocketAddress.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/InetSocketAddress.java,v
retrieving revision 1.7
diff -u -b -B -r1.7 InetSocketAddress.java
--- java/net/InetSocketAddress.java 14 Jan 2003 21:44:48 -0000 1.7
+++ java/net/InetSocketAddress.java 17 Jun 2003 13:52:23 -0000
@@ -70,6 +70,9 @@
if (port < 0 || port > 65535)
throw new IllegalArgumentException();
+ if (addr == null)
+ addr = InetAddress.ANY_IF;
+
this.addr = addr;
this.port = port;
this.hostname = addr.getHostName ();
@@ -85,24 +88,8 @@
public InetSocketAddress(int port)
throws IllegalArgumentException
{
- if (port < 0 || port > 65535)
- throw new IllegalArgumentException();
-
- this.port = port;
-
- try
- {
- byte[] any = { 0, 0, 0, 0 };
- this.addr = InetAddress.getByAddress (any);
- this.hostname = "0.0.0.0";
+ this ((InetAddress) null, port);
}
- catch (UnknownHostException e)
- {
- this.addr = null;
- this.hostname = "";
- }
- }
-
/**
* Constructs an InetSocketAddress instance.
@@ -115,7 +102,8 @@
public InetSocketAddress(String hostname, int port)
throws IllegalArgumentException
{
- if (port < 0 || port > 65535)
+ if (port < 0 || port > 65535
+ || hostname == null)
throw new IllegalArgumentException();
this.port = port;
Index: java/net/Socket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/Socket.java,v
retrieving revision 1.23
diff -u -b -B -r1.23 Socket.java
--- java/net/Socket.java 8 Jun 2003 10:12:09 -0000 1.23
+++ java/net/Socket.java 17 Jun 2003 13:52:23 -0000
@@ -37,7 +37,9 @@
package java.net;
-import java.io.*;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.channels.SocketChannel;
import java.nio.channels.IllegalBlockingModeException;
@@ -87,8 +89,6 @@
private boolean closed = false;
- // Constructors
-
/**
* Initializes a new instance of <code>Socket</code> object without
* connecting to a remote host. This useful for subclasses of socket that
@@ -282,6 +282,9 @@
{
this();
+ if (raddr == null)
+ throw new NullPointerException ();
+
if (impl == null)
throw new IOException("Cannot initialize Socket implementation");
@@ -289,8 +292,12 @@
if (sm != null)
sm.checkConnect(raddr.getHostName(), rport);
- // bind/connect socket
- bind (new InetSocketAddress (laddr, lport));
+ // bind socket
+ SocketAddress bindaddr =
+ laddr == null ? null : new InetSocketAddress (laddr, lport);
+ bind (bindaddr);
+
+ // connect socket
connect (new InetSocketAddress (raddr, rport));
// FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,