Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-481

Possible race-conditions in hashCode of the range classes

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.4
    • 3.0
    • lang.*
    • None

    Description

      The hashCode() methods of the range classes look very suspicious to me. The value is lazily initialized, but the calculation is done _on the cached value. With some unlucky timing a caller may get an incomplete hash.

      An unlucky sequence of Code could be something like
      T1: if (hashCode == 0) // true
      T1: hashCode = 17;
      T2: if (hashCode == 0) // now false because hashCode was already set to 17
      T2: return hashCode; // return 17
      T1: hashCode = 37 * hashCode...........

      where T1 and T2 are different threads accessing the method in parallel and T2 gets the wrong hash "17".

      Affected classes are

      org.apache.commons.lang.math.DoubleRange
      org.apache.commons.lang.math.FloatRange
      org.apache.commons.lang.math.IntRange
      org.apache.commons.lang.math.LongRange
      org.apache.commons.lang.math.NumberRange
      org.apache.commons.lang.math.Range

      Possible fix: calculate the hash on a temporary variable and finally assign it to the member

      Attachments

        1. LANG-481-reordering_datarace.patch
          3 kB
          Boris
        2. LANG-481.patch
          5 kB
          Boris

        Activity

          People

            Unassigned Unassigned
            2091c76f726f21a61b6d2f8b885cc39d Boris
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: