Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8366034

URL equality fails for same URL string

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      I believe that I've spotted the following in my servers:

      ```
      import java.net.URL

      String address = // some http address
      URL firstUrl = new URL(address)
      URL secondUrl = new URL(address)

      logger.info(firstUrl.equals(secondUrl).toString()) // prints false
      ```

      I'm using the deprecated method but it doesn't really matter. My expectation is that in URLStreamHandler, hostEqual getHostAddress gets executed two times, and IP can change in between calls. It doesn't break reflection because the host is cached, but it is definitely surprising. In production with much more traffic this happens quite often.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Set networkaddress.cache.ttl=0 so that Java is never caching
      2. Set a breakpoint in URLStreamHandler.java, in line "InetAddress a2 = getHostAddress(u2);"
      3. Run the test code until the breakpoint
      4. Change the /etc/hosts file overriding the URL from the test code, for example "1.1.1.1 www.google.com"
      5. Resume the program and see the result

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Logger shows "true"
      ACTUAL -
      Logger shows "false"

      ---------- BEGIN SOURCE ----------
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.util.logging.Logger;

      public class Test{

          public static void main(String [] args) throws MalformedURLException{
              final Logger logger = Logger.getLogger(Test.class.getName());
              String address = "https://www.google.com/";
              
              URL firstUrl = new URL(address);
              URL secondUrl = new URL(address);

              logger.info(Boolean.toString(firstUrl.equals(secondUrl))); // prints false
          }
      }
      ---------- END SOURCE ----------

        1. Test.java
          0.5 kB
          Patricia Tavares

            pmtavare Patricia Tavares
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: