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

General contract for hashCode() method is broken in java.net.SocketPermission

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • None
    • 6
    • core-libs
    • x86
    • solaris

      General contract for Object.hashCode() method reads:

      "If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result."

      In java.net.SocketPermission implementation of equals/hashCode methods this contract is broken. For the code below:


              SocketPermission sp1 = new SocketPermission("localhost", "accept");
              SocketPermission sp2 = new SocketPermission("127.0.0.1", "accept");
              System.out.println("sp1.equals(sp2)=" + sp1.equals(sp2));
              System.out.println("sp1.hashCode()=" + sp1.hashCode()
                      + ", sp2.hashCode()=" + sp2.hashCode());

      we can see the following output:

      sp1.equals(sp2)=true
      sp1.hashCode()=-1204607085, sp2.hashCode()=1505998205

      Which is incorrect according to the contract for Object.hashCode(), moreover the same statement is repeated in java.security.Permission.hashCode() specification:
      "If two Permission objects are equal according to the equals method, then calling the hashCode method on each of the two Permission objects must produce the same integer result."

            Unassigned Unassigned
            mismirno Mikhail Smirnov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: