This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

FYI: Fix InetAddress.getByName and loopback issues


Passing null to InetAddress.getByName() and getAllByName should return the loopback InetAddress. Its possible to do this without the normal security check - passing a null argument to SecurityManager would be a NullPointerException. This also cleans up the security checks - lookup() does not need to do a check becuase the public methods that call it will have done it already. In the getByName() case, we were actually doing the check 3 times - once in getByName, once in getAllByName, and then again in lookup.

I'm checking this in.

Regards

Bryce


2004-08-13  Bryce McKinlay  <mckinlay@redhat.com>

	* java/net/InetAddress.java (loopbackAddress): Renamed from 
	localhostAddress.
	(getByName): Return loopback address for null hostname, without
	security check. Use lookup(), not getAllByName.
	(getAllByName): Return loopback address for null hostname, without
	security check.
	* java/net/natInetAddressPosix.cc (lookup): Don't perform security
	check here.

Index: InetAddress.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/InetAddress.java,v
retrieving revision 1.29
diff -u -r1.29 InetAddress.java
--- InetAddress.java	20 Apr 2004 13:05:09 -0000	1.29
+++ InetAddress.java	13 Aug 2004 22:16:52 -0000
@@ -68,7 +68,10 @@
    */
   static InetAddress ANY_IF;
     
-  private static final byte[] localhostAddress = { 127, 0, 0, 1 };
+  private static final byte[] loopbackAddress = { 127, 0, 0, 1 };
+
+  private static final InetAddress loopback 
+    = new InetAddress (loopbackAddress, "localhost");
 
   private static InetAddress localhost = null;
 
@@ -564,7 +567,8 @@
    * default.  This method is equivalent to returning the first element in
    * the InetAddress array returned from GetAllByName.
    *
-   * @param hostname The name of the desired host, or null for the local machine.
+   * @param hostname The name of the desired host, or null for the local 
+   * loopback address.
    *
    * @return The address of the host as an InetAddress object.
    *
@@ -576,14 +580,15 @@
   public static InetAddress getByName(String hostname)
     throws UnknownHostException
   {
+    // If null or the empty string is supplied, the loopback address
+    // is returned. Note that this is permitted without a security check.
+    if (hostname == null || hostname.length() == 0)
+      return loopback;
+
     SecurityManager s = System.getSecurityManager();
     if (s != null)
       s.checkConnect(hostname, -1);
 
-    // Default to current host if necessary
-    if (hostname == null || hostname.length() == 0)
-      return getLocalHost();
-
     // Assume that the host string is an IP address
     byte[] address = aton(hostname);
     if (address != null)
@@ -608,8 +613,9 @@
       }
 
     // Try to resolve the host by DNS
-    InetAddress[] addresses = getAllByName(hostname);
-    return addresses[0];
+    InetAddress result = new InetAddress(null, null);
+    lookup (hostname, result, false);
+    return result;
   }
 
   /**
@@ -620,7 +626,7 @@
    * hostname of the local machine is supplied by default.
    *
    * @param hostname The name of the desired host, or null for the
-   * local machine.
+   * local loopback address.
    *
    * @return All addresses of the host as an array of InetAddress objects.
    *
@@ -632,6 +638,11 @@
   public static InetAddress[] getAllByName(String hostname)
     throws UnknownHostException
   {
+    // If null or the empty string is supplied, the loopback address
+    // is returned. Note that this is permitted without a security check.
+    if (hostname == null || hostname.length() == 0)
+      return new InetAddress[] {loopback};
+
     SecurityManager s = System.getSecurityManager();
     if (s != null)
       s.checkConnect(hostname, -1);
@@ -676,7 +687,7 @@
     // However, if there is a security manager, and the cached result
     // is other than "localhost", we need to check again.
     if (localhost == null
-	|| (s != null && localhost.addr != localhostAddress))
+	|| (s != null && ! localhost.isLoopbackAddress()))
       getLocalHost (s);
     
     return localhost;
@@ -724,7 +735,7 @@
       }
     
     if (localhost == null)
-      localhost = new InetAddress (localhostAddress, "localhost");
+      localhost = new InetAddress (loopbackAddress, "localhost");
   }
 
   /**
Index: natInetAddressPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/natInetAddressPosix.cc,v
retrieving revision 1.3
diff -u -r1.3 natInetAddressPosix.cc
--- natInetAddressPosix.cc	30 Nov 2003 21:02:56 -0000	1.3
+++ natInetAddressPosix.cc	13 Aug 2004 22:16:52 -0000
@@ -229,13 +229,6 @@
     {
       if (!all)
         host = JvNewStringUTF (hptr->h_name);
-      java::lang::SecurityException *ex = checkConnect (host);
-      if (ex != NULL)
-	{
-	  if (iaddr == NULL || iaddr->addr == NULL)
-	    throw ex;
-	  hptr = NULL;
-	}
     }
   if (hptr == NULL)
     {

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]