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

Improve javafx.util.Pair hash code computation

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8u66
    • Fix Version/s: 9
    • Component/s: javafx
    • Labels:
    • Subcomponent:

      Description

      FULL PRODUCT VERSION :


      ADDITIONAL OS VERSION INFORMATION :
      64-bit Ubuntu 14.04

      A DESCRIPTION OF THE PROBLEM :
      The current implementation of the "hashCode" function in the convenience class "Pair" is as follows.

          public int hashCode() {
              // name's hashCode is multiplied by an arbitrary prime number (13)
              // in order to make sure there is a difference in the hashCode between
              // these two parameters:
              // name: a value: aa
              // name: aa value: a
              return key.hashCode() * 13 + (value == null ? 0 : value.hashCode());
          }

      However, this is an incorrect way to compute a hash code of two values. For example, here is a trivial collision of two hash codes which should be different.

          System.out.println(
              new Pair<Integer,Integer>(1,0).hashCode()
                  ==
              new Pair<Integer,Integer>(0,13).hashCode()
          ); // => true

      This can lead to hard-to-find bugs anywhere that instances of Pair are used in a data structure like a HashSet or HashTable.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javafx.util.Pair;
      public class Solution {
          public static void main(String args[] ) throws Exception {
              System.out.println(
                  new Pair<Integer,Integer>(1,0).hashCode()
                      ==
                  new Pair<Integer,Integer>(0,13).hashCode());
          }
      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                vadim Vadim Pakhnushev
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: