Koloboke icon indicating copy to clipboard operation
Koloboke copied to clipboard

IntSet.toIntArray() fails with ArrayIndexOutOfBoundsException in multi-threaded env

Open AMilassin opened this issue 8 years ago • 0 comments

I have encountered an issue where in an incorrectly synchronized multi-threaded environment the IntSet.toIntArray() method call returns an ArrayIndexOutOfBoundsException.

This is not a major issue and it is acceptable as "undefined behavior" while accessing a non thread safe collection without proper guards but throwing a ConcurrentModificationException would be more helpful to find these problems.

Test code

@Test
public void toIntArray_should_not_throw_ArrayIndexOutOfBoundsException() throws Exception {
    final IntSet ids = HashIntSets.newUpdatableSet(1048576);
    final Random r = new Random();
    Thread  thread = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int j = 0; j < 2000000; j++) {
                ids.add(r.nextInt(1000000));
            }
        }
    });
    thread.start();

    for (int j = 0; j < 2000000; j++) {
        ids.add(r.nextInt(1000000));

        if (ids.size() % 1000 == 0) {
            int[] ints = ids.toIntArray();
            System.out.println(ints.length);
        }
    }
}

AMilassin avatar Feb 11 '16 09:02 AMilassin