Uploaded image for project: 'Harmony'
  1. Harmony
  2. HARMONY-6681

AbstractCollection.toArray() and AbstractCollection.toArray(T[]) are broken for concurrently modified collections like ConcurrentHashMap.keySet()



    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 6.0M4, 5.0M16
    • None
    • Classlib
    • None


      If you call ConcurrentHashMap.keySet().toArray() as done in trunks Lucene's RAMDirectory#fileMap and another thread modifies the collecton, you get NoSuchElementExceptions or AIOOBEs. The reason is that AbstractCollection.toArray methods allocate the array using the current size of the collection and then either iterate on the values (which is oficially supported by ConcurrentHashMap, its iterator is safe for concurrent modification!) and set them on the target without checking bounds. On the other hand if there are too few entries in the collection, the array is nulled at the end. The bad thing is that the toArray() and toArray(T[]) methods behave differently, so depending on the modification they throw different exceptions (one impl simply drops entries that dont fit into the array).

      Since Java SE 6, its officially documented, how toArray must behave: http://download.oracle.com/javase/6/docs/api/java/util/AbstractCollection.html#toArray(T[]); This is violated by harmony and even the fix in HARMONY-6236 does not respect it, leading to such bugs.

      Java SE 5 does not document that, but ConcurrentHashMap.keySet(), values() and entrySet() have own implementations of toArray() (which can be found out by reflection), the root AbstractCollection.toArray() is not behaving fine there.

      For harmony the fix would be to correct this issue "correctly" by changing the base AbstractCollection implementation (I would recommend that).

      This is also the root cause for HARMONY-6236 and similar issues, the fix there is invalid, but I cannot reopen the issue.


        Issue Links



              Unassigned Unassigned
              uschindler Uwe Schindler
              0 Vote for this issue
              0 Start watching this issue